compareTo and deriving
forax at univ-mlv.fr
Mon Dec 4 13:34:23 UTC 2017
record/data class currently provides getters and toString/equals/hashCode, what if i want to add a compareTo.
In Haskell, it's easy, one can use 'deriving', in Java, it can be written that way,
record Point(int x, int y) implements Comparable<Point>
deriving equals, hashCode, toString, compareTo;
For the compiler, the method exists in the supertypes, so the signature of the method can be computed from the super types,
after each generated method use an invokedynamic with the same meta protocol (a list of getters),
so if by convention "equals", "hashCode", "toString", "compareTo" are the names of some bootstrap methods in a class DerivingMetaFactory,
it can be trivial for a JDK developer to add a new bootstrap method without having to change the JLS.
So my point is, that we can specify once how the compiler should generate codes for any generated methods in an extensible way.
More information about the amber-spec-observers