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

bitter_fox bitterfoxc at gmail.com
Thu Nov 8 06:40:27 PST 2012


Hi,
Sorry for your confusing by my insufficient explanation.

I think accepting is not a correct behavior because it seems the compiler
doesn't check override of clone of Object.

Object has a protected clone method and F has a public clone method. The
compiler makes "class $$lambda$n implements F"(implicitly it extends
Object).  In this case the Object's protected clone will win. So the
$$lambda$n has a protected clone. This is illegal by JLS[8.4.8.3][1].

JLS[8.4.8.3] Requirements in Overriding and Hiding

"    If the overridden or hidden method is public, then the overriding or
hiding method must be public; otherwise, a compile-time error occurs."

In this case, the overridden method is F#clone and this is public, and the
overriding method is Object#clone and this is not public. This would be
compile-time error.

[1]: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3

Regards,
bitter_fox


2012/11/8 Maurizio Cimadamore <maurizio.cimadamore at oracle.com>

> This glitch is due to the fact that translation code for lambdas whose
> target is an intersection type is not there yet. So, as a short-time hack,
> we adopted the view that the first type of an intersection types dictates
> how lambda conversion should work. This is all temporary and will be
> removed.
>
> Maurizio
>
>
> On 08/11/12 07:45, 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
>>
>>
>


More information about the lambda-dev mailing list