Dónal
Dónal

Reputation: 187499

overload 'in' operator

In Groovy, I can write code such as:

assert 1 in [1,2,3]
assert "foo" in "foo"
assert 2 in 1..4

Is there some way that I can overload the 'in' operator for my own classes? If so, which method should I implement?

Thanks!

Upvotes: 2

Views: 268

Answers (2)

tim_yates
tim_yates

Reputation: 171084

You need to overload the isCase method, ie:

class Example {
  def isCase( o ) {
    true
  }
}

def c = new Example()
println( "woo" in c )

prints true

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

The "in" operator is based on the contains() method.

Upvotes: 1

Related Questions