Different behavior between (F) and (Object & F)

Sergey Kuksenko sergey.kuksenko at oracle.com
Thu Nov 8 06:09:47 PST 2012


Why just not to make all lambdas serializable and forget about that?
Allowing to mark lambda with ZAM will cause a huge usage of ZAM marking 
interfaces in user's code. They get new feature -> they will use it in 
all possible ways. But you may check marking with ZAM only by instanceof 
that is not a good style.

On 11/08/2012 05:45 PM, Brian Goetz wrote:
> Casting a lambda/method ref to (T & U) requires that
>    - T be a functional interface
>    - U be a marker (ZAM) interface
>
> So, the compiler behavior is correct.  However, the error message could
> probably be clearer.
>
> On 11/8/2012 2:45 AM, bitter_fox wrote:
>> Hi,
>> There is a different behavior in the same semantics:
>>
>> public class Main
>> {
>>       interface F
>>       {
>>           void m();
>>           default void clone() throws CloneNotSupportedException {}
>>       }
>>
>>       public static void main(String[] args)
>>       {
>>           Object o;
>>
>>           // implicit Object
>>           o = (F) () -> {}; // pass compiler check
>>
>>           // explicit Object
>>           o = (Object & F) () -> {}; // compile time error
>>       }
>> }
>>
>> Compiler rejects "(Object & F) () -> {}", but it accepts "(F) () -> {}".
>> Is this the correct behavior?
>>
>> This is the compile error by "(Object & F) () -> {}":
>>
>> Main.java:18: error: clone() in Object cannot implement clone() in F
>>                   o = (Object & F) () -> {}; // compile time error
>>                       ^
>>     attempting to assign weaker access privileges; was public
>> 1 error
>>
>> Regards,
>> bitter_fox
>>
>


-- 
Best regards,
Sergey Kuksenko


More information about the lambda-dev mailing list