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

Brian Goetz brian.goetz at oracle.com
Thu Nov 8 05:45:46 PST 2012


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
>


More information about the lambda-dev mailing list