jcoplien / trygve

The trygve language project - Building a DCI-centric language from the ground up
GNU General Public License v2.0
104 stars 15 forks source link

compareTo not properly being invoked for invocation of X.equals(x) by libraries such as List<X> #91

Closed jcoplien closed 8 years ago

jcoplien commented 8 years ago

Libraries such as string and map implement their contains() (e.g.) methods by searching for a value match according to a supplied compareTo function. For user-supplied trygve types on a List or in another such data structure, the programmer-supplied compareTo method is not being consulted in this regard.

Test program from Héctor:

class Number { int number public Number(int num) { number = num.clone } public int value() const { return number } public int compareTo(Number other) const { return if(value() > other.value()) 1 else if (value() == other.value()) 0 else -1 } public Number +(Number other) const { return new Number(number + other.number) } public boolean equals(Number n) { return (compareTo(n) == 0) } public String toString() const { return number.toString() } } { List ln = new List() Number five = new Number(5) ln.add(five) System.out.println(ln.contains(five)) // <-- true System.out.println(ln.contains(new Number(5))) // <-- false }

jcoplien commented 8 years ago

Fixed in 1.6.2