Details
-
Bug
-
Status: Closed
-
Blocker
-
Resolution: Won't Fix
-
1.1-rc-3
-
None
-
None
-
None
Description
def g = 'g' def string = 'groovy' def gString = "${g}roovy" println gString.equals(string) // false println gString == string // true
For classes implementing the Comparable interface, the result of == is based on the value of the compareTo method, not on the result of the equals method.
Beside the fact that gString.equals(string) not being true for the example above is unintuitive and leads to obvious problems with collections, the fact the compareTo and thus gString == string behaves differently is a BIG problem. It does not follow the strong recommendation of the Comparable interface (http://java.sun.com/j2se/1.5.0/docs/api/) which can lead to big problems.
Here is the code from the GString class.
// public boolean equals(Object that) { if (that instanceof GString) { return equals((GString) that); } return false; } public boolean equals(GString that) { return toString().equals(that.toString()); } public int compareTo(Object that) { return toString().compareTo(that.toString()); }