Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda and cast intersection type (Eclipse compiler bug)

So, why does this code compile?

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 {

}

This is sort of misleading, I do know about casting to an intersection type, I've read the jls, but still confusing.

It sort of makes sense why

 Serializable & Comparable 

would compile, since the result (the synthetic type) between those two is a functional interface, but why does :

 Calculator & Sumator 

works? That synthetic type is not a functional interface.

like image 287
Eugene Avatar asked Apr 08 '26 03:04

Eugene


1 Answers

This was a bug in Eclipse which has been fixed for milestone 6 towards Eclipse Neon (4.6).

like image 96
Stephan Herrmann Avatar answered Apr 10 '26 18:04

Stephan Herrmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!