`this` in concise method bodies

Remi Forax forax at univ-mlv.fr
Fri Oct 12 19:58:08 UTC 2018

> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "daniel smith" <daniel.smith at oracle.com>, "amber-spec-experts"
> <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 12 Octobre 2018 19:15:53
> Objet: `this` in concise method bodies

> On 10/5/2018 7:22 PM, Dan Smith wrote:

>> - It's not totally clear what we do with 'this'. Kind of seems like it should be
>> treated as the first parameter to be passed, but there are also examples in the
>> JEP that ignore it. And is it allowed to be referenced by the receiver
>> expression?

>> void reverse() = Collections::reverse; // invoke Collections.reverse(this)?
>> int length(String s) = String::length; // invoke s.length()?
>> void altMethod() = ThisClass::method; // invoke this.method()?
>> void altMethod2() = this::method; // legal?

> Drilling in on `this`:

> One of the strongest motivating examples was:

> static final Comparator c = ...

> int compareTo(T other) = c::compare;

> And in this case, we want this to wire to c.compare(this, other).

> However, another strong motivating case is:

> final List myList = ...

> int size() = myList::size;

> in which case we want this to wire to myList.size() and ignore `this` entirely.

> And, both use cases are equally desirable.

1) They are few methods that can be converted to concise methods because the implementation has to be a one liner, 
2) They are very few methods that can be converted to a concise methods that that use the :: syntax because it only works if the implementation delegates to a method that uses all the parameters in the same order, 
3) They are very very few methods that can be converted to a concise methods that use the :: and use 'this' explicitly as first parameter. 

so before descending into the rabbit hole, i think not me|someone should check how many methods can be converted to use the :: + this syntax in big corpus of Java codes. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20181012/fcdaf170/attachment.html>

More information about the amber-spec-experts mailing list