Function types versus arrays

Rémi Forax forax at univ-mlv.fr
Mon Feb 8 16:05:10 PST 2010


Le 09/02/2010 00:40, Neal Gafter a écrit :
> On Mon, Feb 8, 2010 at 2:47 PM, Joshua Bloch<jjb at google.com>  wrote:
>    
>> I just assumed that we'd do whatever it takes to make this works, up to and
>> including VM changes.
>>      
> I'd love to hear ideas about how to make it work.  The only way I know
> to make it work would be to reify generics.  Other than that, and
> absent any well founded ideas, there isn't anything to do.
>
>    

Or choose to use method handles :)
In that case, the rules are the same as the ones for generics.
The common supertype of all function types is java.dyn.MethodHandle
- you can't create an array of function types
- you can create an array of MethodHandle
- casting an array of MethodHandle to an array of function types
   is an unsafe conversion.

In that case, the following code is legal:
   #int(int)[] array = (#int(int)[])new MethodHandle[10];
but the cast will raise a warning.
Because function type are translated at compile time
to MethodHandle, no ClassCastException at runtime.

BTW, I see two use cases for array of function types,
the first one is if someone want to simulate something like a vtable.
The second one is if you want an array of getter or an array of setters.

Rémi


More information about the lambda-dev mailing list