jdk8 lambda的方法引用引起的编译器bug
2017-09-30 13:26
561 查看
jdk8 lambda的方法引用引起的编译器bug
当使用class::method这种引用方式的时候,如果receiver类有多个super type,在编译的时候没有问题,但是在运行的时候就会异常
Invalid receiver type class...。
解决方法是:
改为lambda表达式。
原文:
You ran into the same compiler bug that has been discussed in this question and that >question.
The problem occurs whenever an intersection type is involved and you are using a method >reference using a receiver type other than the first one (the first type is the one that will >remain after type erasure).
So when you replace the method reference with a lambda expression, you are not affected by >the bug anymore. If you remove the Serializable from the types instead, the inferred element >type of the Stream will be Fruit, i.e. not an intersection type, and again the problem does >not occur. But with the two element types implementing Fruit and Serializable, the compiler >will infer the element type Object&Fruit&Serializable and the raw type will be Object which >provokes the error when using a method reference with the receiver type Fruit. You can easily >work around this:
Stream.of(apples.stream(), oranges.stream())
.flatMap(Function.identity())
.map(Fruit::getPickingMonth) // no more exception on this line
.forEachOrdered(System.out::println);
The compiled code will be identical to your original, but the formal result type of the >flatMap operation will be Stream, ignoring all other artifacts of the inferred >intersection type. As a consequence the method reference Fruit::getPickingMonth will >implement the type Function
相关文章推荐
- IE8中Jquery slideUp、slideDown方法由Position属性引起的Bug
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法
- Java笔记——Java8特性之Lambda、方法引用和Streams
- java8新特性总结——lambda表达式之方法引用与构造器引用
- Java 8 新特性,Lambda,方法引用,Stream,Optional
- JavaFX编译器编译重载方法的一处BUG
- JDK8新特性之方法引用
- [java8] lambda表达式、函数式接口和方法引用
- JavaFX编译器编译满载方法的一处BUG
- 由String作为方法参数,引起的值传递,引用传递,及StringBuffer 原文引用: http://www.cnblogs.com/zuoxiaolong/p/lang1.html ht
- Lambda对方法和构造器的引用
- 再次揭露text/vnd.wap.wml引起的ASP.NET OutputCache Bug及解决方法
- java lambda表达式和方法引用
- Java8 Lambda表达式 函数式编程 方法引用
- Java8函数式编程 Functionallnterface注解、lambda表达式、方法的引用
- Java笔记——Java8特性之Lambda、方法引用和Streams
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
- 应对vs更新程序引用集的小bug问题的方案 很不可思议的解决方法