Serialization opt-in syntax (again)

Remi Forax forax at
Mon Oct 8 15:42:36 PDT 2012

On 10/08/2012 09:17 PM, Brian Goetz wrote:
>> I'm not against the idea to add a way to use & between types in Java.
>> But there are several issues with the cast and the generics that already exist in Java,
>> by example, a cast like (Foo<Bar> & Serializable) is unsafe and mixing type variable like T, U
>> etc as bound of type variable is restricted.
> I am not sure what you mean, can you give an example?

Foo<Bar> is not a reified type, so Foo<Bar> & Serializable is not a 
reified type too.

You can't mix type variable and classical type when you specify a bound 
of a type parameter,
e.g.. this doesn't compile:
   <T, E extends T & Serializable> void foo() { ... }

Another weird example, with <T extends Object & Serializable> a cast to 
T is erased to (Object) and
with <T extends Serializable & Object>, a cast to T is erased to 

To sumarize, A & B is not a reified type.

>> The other problem is how do you handle the SAM conversion is you have a code that use a cast like this:
>>   Object o = (I & J) () -> { ... };
>> at runtime, given that the meta-factory has only one SAM type.
> So, in the context of a lambda, it would be a well-formed target type if it is of the form SAM&ZAM&ZAM...  Arbitrary intersections of interfaces obviously would not work.  If the compiler does not know what J is, then the compiler would not be able to extract enough target type information and would signal an error.

in that case you're reifying a non reified type, I'm not sure there is 
an issue here but
this is clearly a big departure from how the Java type system currently work
and I don't think we have time to study that beast.

Another way to see the problem is that i don't think you can use & with 
the inner class syntax,
   new Object & Serializable() { ... }
for the same reason you can't use the diamond syntax with inner classes.


More information about the lambda-spec-experts mailing list