How does the Groovy in operator work?

隐身守侯 提交于 2019-12-01 03:14:01
Dónal

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.

noah

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!