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
}
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
}