1

Please have a look at this simple code:

String.metaClass {
  getA = {delegate <<= delegate}
}

assert 'a'.a == 'a'.a
Result: Assertion failed: 
    assert 'a'.a == 'a'.a
               | |      |
               | false  aa
               aa

Why the result is FALSE?

I am working at Groovy 2.4.7 on Windows 7 SP1

Axe
  • 45
  • 1
  • 4

1 Answers1

0

LeftShift on a String in Groovy generates a StringBuffer (docs)

So 'a'.a == 'a'.a is trying to do equals on two StringBuffers.

You can't do that with StringBuffer as the equals method isn't overloaded, and just checks if they are the same instance

Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338