Details
-
Sub-task
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.5.7, 1.6-rc-3
-
None
-
None
-
Patch
Description
Once a class implements Comparable, the == operator does not work anymore!
class X implements Comparable { int compareTo(Object object) { return 0 } boolean equals(Object object) { return true; } } public void testEqualityOperator() { def x = new X() def y = new Y() assert x == x // OK assert x == y // OK assert x == 1 // FAIL - Neither compareTo() nor equals() is called! }
The location of the bug is in
DefaultTypeTransform.compareToWithEqualityCheck(Object left, Object right, boolean equalityCheckOnly): if (equalityCheckOnly) { return -1; // anything other than 0 }
The fix is:
if (equalityCheckOnly) { return ((Boolean) InvokerHelper.invokeMethod(left, "equals", right)).booleanValue() ? 0 : -1; }
An even better fix would be to always use equals for the == operator - but this is already discussed in other issues.
I've set this to a BLOCKER beause this issue leads to the fact that I cannot use Groovy for relaxed typing - and that makes it useless for many DSLs!