Lambda和cast交点类型(Eclipse编译器bug) [英] Lambda and cast intersection type (Eclipse compiler bug)

查看:451
本文介绍了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; //编译,但抛出一个异常
sumator.test();

both =(Both)(a,b) - > a + b; //不编译
}

接口计算器{
public int calculate(int a,int b);
}

接口Sumator {
public int test();

public int test3(int a,int b);
}

//两种类型的交集
接口两个扩展Sumator,Calculator {

}

这是一种误导,我知道关于转换为交叉路口类型,我已经阅读了jls,但仍然令人困惑。



这对于

 可序列化&可比较的

将编译,因为这两者之间的结果(合成类型)是一个功能界面,为什么:

  Calculator& Sumator 

工程?该合成类型不是功能界面。

解决方案

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


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.

解决方案

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

这篇关于Lambda和cast交点类型(Eclipse编译器bug)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆