【发布时间】:2019-03-21 16:31:46
【问题描述】:
使用 java 8 编译器,这个程序:
import java.util.function.Consumer;
public class xx {
public void execute(Consumer<? super Runnable> executor, Runnable action) {
executor.accept(() -> action.run());
}
}
失败:
xx.java:4: error: incompatible types: <captured wildcard> is not a functional interface
executor.accept(() -> action.run());
^
显然编译器无法推断() -> action.run()的类型。
通过将 ? super Runnable 更改为 Runnable 或将 lambda 显式转换为 Runnable,这很容易解决。
我的问题:这个失败似乎有点“愚蠢”.. 这种行为是由 JLS 指定的,还是编译器错误?
【问题讨论】: