compareTo and deriving
forax at univ-mlv.fr
forax at univ-mlv.fr
Tue Dec 5 08:12:34 UTC 2017
So let restart by trying to untangle things.
First question, should we only support Object methods (toString, equals, hashCode) or more, again here, two sub questions,
it can be only (toString, equals, hashCode) in the first release and more in a subsequent release.
Why more, the question is where to put the bar, if we do only toString, equals, hashCode, people will ask for compareTo (i don't think there is another method than compareTo but i may be wrong). The rational to introduce compareTo is that the semantics of compareTo is linked to the semantics of equals which is generated, so asking someone to write compareTo given that he doesn't have to write equals is clunky. There are several reason to not introduce compareTo. toString, equals, hashCode cover most of the case and we have to stop somewhere, people tend to use compareTo at a place where they should use a Comparator, i.e. the order defined by compareTo is not a 'natural' order.
Second question, how to specify the mapping between toString, equals, hashCode and their implementation ? Should we have a way to extends the mapping if we want to introduce compareTo later without having to change the JLS. Again, sub-questions, Should this mapping being explicit like in Haskell with deriving or should it be implicit, there is no real need to specify a mapping for the method for j.l.Object because those method declaration are already present, and if the record implements Comparable it means that we have to provide the implementation of compareTo.
----- Mail original -----
> De: forax at univ-mlv.fr
> À: "Brian Goetz" <brian.goetz at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Lundi 4 Décembre 2017 16:12:58
> Objet: Re: compareTo and deriving
> ----- Mail original -----
>> De: "Brian Goetz" <brian.goetz at oracle.com>
>> À: "Remi Forax" <forax at univ-mlv.fr>, "amber-spec-experts"
>> <amber-spec-experts at openjdk.java.net>
>> Envoyé: Lundi 4 Décembre 2017 15:54:13
>> Objet: Re: compareTo and deriving
>> As you might remember from the EG meeting, we have toyed with a notion
>> of "method builders" which would be a more declarative way to declare
>> template-izable methods. The intent all along was that, once we have
>> such a mechanism, records would just be a consumer of such a mechanism.
>> That said, such a mechanism is bigger both from a technical perspective
>> and a bikeshed perspective. 90% of the current OO-ceremony-pain is in
>> ctors, Object methods, and accessors. So my preference would be to nail
>> down the semantics of data classes first, and then explore whether it
>> makes sense to surface a more general mechanism.
> I'm trying to propose a middle ground (not too far from only support Object
> methods) to allow to not have too much couple between the JLS and the
> metafactory methods.
> One advantage i see to have compareTo generated is that most implementation i
> see of the compareTo are broken either because of the primitive overflowing or
> compareTo and equals not having compatible semantics. One problem we will have
> if we let people to write compareTo is that it will be harder to get it right
> because equals will be generated while compareTo will be written manually
> without seeing the code of equals.
>> On 12/4/2017 8:34 AM, Remi Forax wrote:
>>> 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
>>> 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