Method and Field Literals

forax at univ-mlv.fr forax at univ-mlv.fr
Tue Jun 19 22:09:36 UTC 2018


> De: "John Rose" <john.r.rose at oracle.com>
> À: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Brian Goetz" <brian.goetz at oracle.com>, "discuss" <discuss at openjdk.java.net>
> Envoyé: Mardi 19 Juin 2018 22:50:46
> Objet: Re: Method and Field Literals

> On Jun 19, 2018, at 11:40 AM, Remi Forax < [ mailto:forax at univ-mlv.fr |
> forax at univ-mlv.fr ] > wrote:

>> BTW, if we want to support MethodHandle (or j.l.r.Method) at some point, we
>> should re-use the same idea we use for the Serialization, use a
>> non-conventional cast:
>> MethodHandle mh = (MethodHandle & Predicate<String>)String::isEmpty;

>> with Predicate<String> used only to provide the necessary type information, this
>> has the advantage of not requiring to change the grammar thus lowering the cost
>> to introduce such feature.

> After the above code executes I would *not* expect the following to be true:

> assert(mh instanceof Predicate);

> So there's something fishy about the cast to the conjunctive type: It doesn't
> yield an expression value of the type claimed by the cast.

The MH can be an instanceof of Predicate by resurrecting the user-defined MethodHandle we have buried during the JSR 292, but it will complicate all JSR 292 implementations for no gain. 
So yes, the mh should not be an instanceof the target type. 

Technically, the cast to a target type is not really a cast even today, a cast take an instance and return an instance, the constant method reference/lambda cast is an eta transformation that take nothing (from the stack) and supply an instance. 

> One possible fix: Declare that such casts are themselves poly expressions,
> to the extent that they are allowed to drop the "inconvenient" part of the type,
> for certain target types like MH.

I'm not sure that a poly-expression can drop a constraint. I still think it should be seen as an eta transformation like any other constant lambda/method reference creation. 
At runtime, it's a ldc to a ConstantDynamic that returns either a MethodHandle or a j.l.r.Method (extracted from the MH via a MethodHandleInfo). 

> — John

Rémi 


More information about the discuss mailing list