java.lang.Class literals

Peter Levart peter.levart at gmail.com
Tue Aug 11 20:34:14 UTC 2015


Hi,

Given:

class Single<any T> {}

We can express j.l.Class literals representing specialized classes with:

Single<boolean>.class
Single<byte>.class
Syngle<char>.class
...

We can express the Class representing common interface implemented by 
specialized and raw classes with:

Single<any>.class

And we can express the Class representing raw class itself with:

Single.class

The following are not possible:

error: illegal generic type for class literal
         test(Single<?>.class);

error: illegal generic type for class literal
         test(Single<Object>.class);

But that's OK, since they all represent the same j.l.Class as 
Single.class and we don't need many ways to express the same thing.



But let us take the following:

class Double<any T, any U> {}

Class literals for full specializations (where both type parameters are 
value types) are possible to express:

Double<byte, byte>.class);
Double<byte, char>.class);
Double<char, byte>.class);
Double<char, char>.class);
...
...

So is the common interface implemented by all classes (specialized and raw):

Double<any, any>.class

What's interesting is that there is no special run-time representation 
for a type inbetween:

Double<byte, byte>  <:  Double<any, byte>  <: Double<any, any>

Double<any, byte> gets "erased" to Double<any, any>.class, but we can 
still write Double<any, byte>.class which just represents an alias for 
Double<any, any>.class.

There's also the raw class representation which is used for 
all-reference type var instantitations:

Double.class

But I couldn't find a way to express a Class literal representing class 
used at runtime for the following type, for example:

Double<Object, int>

error: illegal generic type for class literal
         test(Double<?, any>.class);


It's easy to work-around this limitation:

     static class Double<any T, any U> {
         static <any T, any U> Class<?> clazz() {
             return Double<T, U>.class;
         }
     }

...and use:

Double.<Object, byte>clazz()  instead of  Double<?, byte>.class


So I wonder if that Class literal limitations could be lifted and allow 
the following:

     Double<?, int>.class
     Double<int, ?>.class
     ...

And, for consistency, also the following:

     Double<?, ?>.class (which would be another way to say Double.class)


What do you think?

Regards, Peter




More information about the valhalla-dev mailing list