编译错误:Lambda目标类型交点类型 [英] Compilation Error: Lambda Target Type Intersection Type

查看:193
本文介绍了编译错误:Lambda目标类型交点类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class X {
Object o =(I& J)() - > {};
}
接口I {
public void foo();
}
接口J {
public void foo();
public void bar();
}

Oracle编译器抛出错误:

  X.java:2:错误:不兼容类型:INT#1不是功能界面
Object o =(I& J)() - > ; {};
^
在接口INT#1
中找到的多个非重载抽象方法其中INT#1是交集类型:
INT#1扩展Object,I,J
1错误

Eclipse编译器编译正常。



上述示例的修改形式:

  public class X {
Object o =(I& J)() - > {};
}
接口I {
public void foo();
}
接口J {
public void foo();
}

Eclipse编译器抛出一个错误。 Oracle编译器接受它。



我认为Oracle编译器是正确的。



考虑测试用例:

  interface I {
default void foo(){System.out.println(foo I \\\
); }
default void bar(){System.out.println(bar I \\\
); }
}
interface J extends I {
default void foo(){System.out.println(foo J \\\
); }
}
public class Y {
public static void main(String argv [])throws Exception {
J j = new J(){
};

((I& J)j).foo();
((I& J)j).bar();
}

}



Oracle和Eclipse编译器的输出是:

  foo J 
bar I

根据输出,我可以得出结论,Oracle看起来正确。



让我知道你们如何解释它。



谢谢

解决方案

在第一个例子中, I& J 不是功能界面(只有一个抽象非对象方法)。所以javac是错误的。



在第二种情况下, I& J 是一个功能界面,所以javac再次是正确的。



听起来像Eclipse编译器中的两个错误。


public class X {
  Object o = (I & J) () -> {};
}
interface I {
   public void foo(); 
}
interface J {
   public void foo();
   public void bar(); 
}

Oracle compiler throws a error:

 X.java:2: error: incompatible types: INT#1 is not a functional interface
  Object o = (I & J) () -> {};
                     ^
multiple non-overriding abstract methods found in interface INT#1
where INT#1 is an intersection type:
INT#1 extends Object,I,J
1 error

Eclipse compiler compiles fine.

Which implementation looks correct ?

A modified form of the above example:

public class X {
  Object o = (I & J) () -> {};
}
interface I {
   public void foo(); 
}
interface J {
   public void foo();
}

Eclipse compiler throws a error. Oracle compiler accepts it.

I think Oracle compiler is correct.

Consider the test case:

interface I {
  default void foo() { System.out.println("foo I \n"); }
  default void bar() { System.out.println("bar I \n"); }
}
interface J extends I {
   default void foo() { System.out.println("foo J \n"); }
 }
public class Y {
    public static void main(String argv[]) throws Exception {
        J j = new J() {
        };

        ((I & J) j).foo();
        ((I & J) j).bar();
    }

}

The output with Oracle and Eclipse Compiler is:

foo J
bar I

Based on the output I can conclude that Oracle looks correct.

Let me know how you guys interpret it.

Thanks

解决方案

In the first example, I&J is not a functional interface (interface with exactly one abstract non-Object method). So javac is correct to give an error.

In the second case, I&J is a functional interface, so javac is again correct.

Sounds like two bugs in the Eclipse compiler.

这篇关于编译错误:Lambda目标类型交点类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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