New JEP: Concise Method Bodies

Remi Forax forax at univ-mlv.fr
Fri Oct 5 20:50:38 UTC 2018


> De: "Kevin Bourrillion" <kevinb at google.com>
> À: "Alex Buckley" <alex.buckley at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 5 Octobre 2018 22:03:53
> Objet: Re: New JEP: Concise Method Bodies

> On Mon, Sep 24, 2018 at 10:17 AM Alex Buckley < [ mailto:alex.buckley at oracle.com
> | alex.buckley at oracle.com ] > wrote:

>> On 9/21/2018 4:07 PM, Kevin Bourrillion wrote:
>> > "The method reference form can use most kinds of method references after
>> > the = sign: static and unbound method references, bound method
>> > references (if the receiver *variable* is in scope for the method
>> > declaration)"

>> > Can we still bind to any expression?

>> > private String a(String b, int c)
>> > throws SomeCheckedException = *d.e()*::f;

>> No limitation is intended on the receiver, as long as the method
>> reference expression would be legal if it appeared in the traditional
>> body of method `a`. (Details of this translation remain to be worked out.)

> Can the expression before the :: refer to any method parameters? Of course, it
> would still expect to pass those parameters into the method, so it's weird to
> have the same parameter used in both ways, but does it make sense to forbid it?

Let say you have a dynamic language where the function are itself object, you may want to write something like this: 

public final class DynFunction { 
private final MethodHandle mh; 

DynFunction(MethodHandle mh) { 
this.mh = mh; 
} 

@Override 
public boolean equals(Object obj) { 
if (!(obj instanceof DynFunction)) { 
return false; 
} 
return mh == ((DynFunction) obj).mh; 
} 

@Override 
public int hashCode() { 
return mh.hashCode(); 
} 

static <R> R call(DynFunction self, Object arg1) throws Throwable = self.mh::invoke; 
static <R> R call(DynFunction self, Object arg1, Object arg2) throws Throwable = self.mh::invoke; 

public static void main(String[] args) { 
try { 
var mh = lookup().findVirtual(DynFunction.class, "equals", methodType(boolean.class, Object.class)); 
var function = new DynFunction(mh); 
boolean result = call(function, function); 
System.out.println("equals " + result); 
} catch(Error | RuntimeException e) { 
throw e; 
} catch(Throwable t) { 
throw new UndeclaredThrowableException(t); 
} 
} 
} 

Rémi 

> --
> Kevin Bourrillion | Java Librarian | Google, Inc. | [ mailto:kevinb at google.com |
> kevinb at google.com ]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20181005/0cdb26fe/attachment-0001.html>


More information about the amber-spec-experts mailing list