Hlaaftana
Hlaaftana

Reputation: 58

Groovy in operator not seeming to call isCase

From what I've read, the Groovy in operator calls isCase. However, I've had a little problem recently and it goes like this:

class Test {}

new Test() in Test

This returns true. But when I switch it up to be:

class Test {}

new Test().isCase(Test)

It returns false. I don't understand the reasoning and cause behind this exactly, and I'm sort of afraid the in operator is hardcoded.

Upvotes: 1

Views: 516

Answers (1)

Sandeep Poonia
Sandeep Poonia

Reputation: 2188

in is a membership operator and is equivalent to calling isCase() method. So according to groovy doc:

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)

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

So in your case it would be:

Test t = new Test()

println t in Test
println Test.isCase(t)

And it would print true in both cases. You are getting false as you are calling the method on wrong object: t.isCase(Test)

Upvotes: 2

Related Questions