New JEP: Concise Method Bodies
Remi Forax
forax at univ-mlv.fr
Tue Oct 9 18:00:09 UTC 2018
Hi Daniel,
----- Mail original -----
> De: "daniel smith" <daniel.smith at oracle.com>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Mardi 9 Octobre 2018 18:31:48
> Objet: Re: New JEP: Concise Method Bodies
>> On Oct 8, 2018, at 12:14 PM, Dan Smith <daniel.smith at oracle.com> wrote:
>>
>> - Type-qualified method ref: if you don't pass 'this' to the method, you're
>> throwing it away. Static utility methods could be referenced with this form.
>> Methods of the current class/supers could be referenced, too, but 'this::foo'
>> and 'super::foo' serve the same purpose, if we allow them.
>>
>> - Expression-qualified method ref: very possibly, the expression preserves the
>> information you need from 'this', and passing 'this' to the method would be
>> redundant. Hard to think of examples in which a user would want 'this' to be
>> passed as a first parameter.
>
> Found a use case:
>
> class Foo implements Comparable<Foo> {
>
> final Comparator<Foo> comp = ...;
>
> int compareTo(Foo arg) = comp::compare;
>
> }
>
> This may turn out to be a pretty important use case...
>
> Where that leaves us is that maybe "try both" is the best strategy.
Aside, from the semantics point of view, this is a very bad example, implementing Comparable means you have a natural order for the class and taking a Comparator as parameter means there is no natural order.
There are several factors against the method ref syntax
- we don't need it because we have the lambda syntax which already exists in Scala, Kotlin or C#.
- unlike when the method ref is an expression, the concise method ref syntax still need to specify the name and the type of the parameters so the syntax is not very short compared to the lambda syntax.
- a method ref can be weird/unreadable.
The main issue with the 'try both' strategy is that we already support two kinds of method ref, static method and instance method, so if the compiler also tests the two kind of strategy, it means that we have a 2x2 matrix of possible signatures to match, this introduced too many possibilities for a feature that is supposed to be readable for everyone.
I think we should stick with the functional interpretation, i.e. 'this' is not part of the argument that are propagated, if you want to delegate to an instance method inside the current class, you can use this::foo, otherwise use a lambda instead.
regards,
Rémi
More information about the amber-spec-experts
mailing list