Groovy in 运算符如何工作?

发布于 2024-08-18 15:07:58 字数 230 浏览 8 评论 0原文

Groovy“in”运算符在不同情况下似乎意味着不同的东西。有时x in y表示y.contains(x),有时它似乎调用y.isCase(x)

Groovy 如何知道要调用哪一个? Groovy 是否知道使用 .contains 方法的特定类或类集?或者该行为是由其中一个对象上的方法的存在触发的?是否存在 in 运算符完全更改为其他内容的情况?

The Groovy "in" operator seems to mean different things in different cases. Sometimes x in y means y.contains(x) and sometimes it seems to call y.isCase(x).

How does Groovy know which one to call? Is there a particular class or set of classes that Groovy knows about which use the .contains method? Or is the behavior triggered by the existence of a method on one of the objects? Are there any cases where the in operator gets changed into something else entirely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-08-25 15:07:58

我做了一些实验,看起来 in 运算符仅基于 isCase 方法,如以下代码所示

class MyList extends ArrayList {
    boolean isCase(Object val) {
        return val == 66
    }
}

def myList = new MyList()
myList << 55
55 in myList // Returns false but myList.contains(55) returns true     
66 in myList // Returns true but myList.contains(66) returns false

对于 JDK 集合类,我想它看起来就像in 运算符基于 contains(),因为 isCase() 为这些类调用 contains()

I did some experimentation and it looks like the in operator is based on the isCase method only as demonstrated by the following code

class MyList extends ArrayList {
    boolean isCase(Object val) {
        return val == 66
    }
}

def myList = new MyList()
myList << 55
55 in myList // Returns false but myList.contains(55) returns true     
66 in myList // Returns true but myList.contains(66) returns false

For the JDK collection classes I guess it just seems like the in operator is based on contains() because isCase() calls contains() for those classes.

吃→可爱长大的 2024-08-25 15:07:58

in 是“成员资格运算符”。

来自 Groovy 3 的文档< /a> (强调我的):

8.6。会员运营商

成员资格运算符 (in) 相当于调用 isCase
方法。 List的上下文中,它相当于调用
包含
,如以下示例所示:

def list = ['Grace','Rob','Emmy']
断言(列表中的“艾美奖”)#(1)    

(1) 相当于调用 list.contains('Emmy')list.isCase('Emmy')

因此,Groovy 总是调用 isCase,在 List 的情况下映射到 contains

in is the "Membership operator".

From the documentation for Groovy 3 (emphasis mine):

8.6. Membership operator

The membership operator (in) is equivalent to calling the isCase
method. In the context of a List, it is equivalent to calling
contains
, like in the following example:

def list = ['Grace','Rob','Emmy']
assert ('Emmy' in list)           # (1)    

(1) equivalent to calling list.contains('Emmy') or list.isCase('Emmy')

So, Groovy always calls isCase, which in case of a List maps to contains.

在你怀里撒娇 2024-08-25 15:07:58

其实都是基于isCase的。 Groovy 添加了 基于 contains 方法的 Collections 的 isCase 方法。任何具有 isCase 的类都可以与 in 一起使用。

It's actually all based on isCase. Groovy adds an isCase method to Collections that is based on the contains method. Any class with isCase can be used with in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文