Lambda和cast交叉类型(Eclipse编译器bug)

那么,为什么这段代码会编译?

public static void main(String[] args) { Calculator test = (Calculator & Sumator) (a, b) -> a + b; System.out.println(test.calculate(2, 3)); Sumator sumator = (Calculator & Sumator) (a, b) -> a + b; // does compile, but throws an Exception sumator.test(); Both both = (Both) (a, b) -> a + b; // does not compile } interface Calculator { public int calculate(int a, int b); } interface Sumator { public int test(); public int test3(int a, int b); } // intersection of both types interface Both extends Sumator, Calculator { } 

这有点误导,我知道关于转换为交叉类型,我读过jls,但仍然令人困惑。

这有点理由

  Serializable & Comparable 

会编译,因为这两者之间的结果(合成类型)是一个function接口,但为什么:

  Calculator & Sumator 

作品? 该合成类型不是function接口。

这是Eclipse中的一个错误 ,已针对Eclipse Neon(4.6)修复了里程碑6。

好吧,它似乎是一个Eclipse编译器错误,因为它不能在javac下编译。 我想知道如何解决这个问题,或者是否已经开启了这个问题。

  MultipleCastsBound.java:5: error: incompatible types: INT#1 is not a functional interface Calculator test = (Calculator & Sumator) (a, b) -> a + b; ^ multiple non-overriding abstract methods found in interface INT#1 where INT#1 is an intersection type: INT#1 extends Object,Calculator,Sumator MultipleCastsBound.java:8: error: incompatible types: INT#1 is not a functional interface Sumator sumator = (Calculator & Sumator) (a, b) -> a + b; ^ multiple non-overriding abstract methods found in interface INT#1 where INT#1 is an intersection type: INT#1 extends Object,Calculator,Sumator 2 errors