views:

250

answers:

2

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?

+1  A: 

I did some experimentation and it looks like the in operator is based on the inCase 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 // return false but myList.contains(55) returns true
66 in myList // returns true but myList.contains(55) 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.

Don
Ok, that makes sense. I was confused about the relationship between "in" and isCase. I can see now contains makes sense when using a container as a case label. Reusing isCase for the in operator is a little surprising though, since it allows for unexpected expressions like 'asdf' in String == true.
ataylor
A: 

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.

noah