> De: "Kevin Bourrillion" <[email protected]> > À: "Alex Buckley" <[email protected]> > Cc: "amber-spec-experts" <[email protected]> > 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:[email protected] > | [email protected] ] > 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:[email protected] | > [email protected] ]
