l4m*_*mpi 19 java lambda java-9
以下代码使用Java 8编译,但不编译Java 9:
public class CompileErrJdk9 {
@FunctionalInterface public interface Closure<R> {
R apply();
}
@FunctionalInterface public interface VoidClosure {
void apply();
}
static <R> R call(Closure<R> closure) {
return closure.apply();
}
static void call(VoidClosure closure) {
call(() -> { closure.apply(); return null; });
}
static <T> void myMethod(T data) {
System.out.println(data);
}
public static void main(String[] args) {
call(() -> myMethod("hello")); //compile error in jdk9
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
CompileErrJdk9.java:24: error: incompatible types: inference variable R has incompatible bounds
call(() -> myMethod("hello")); //compile error in jdk9
^
upper bounds: Object
lower bounds: void
where R is a type-variable:
R extends Object declared in method <R>call(Closure<R>)
1 error
Run Code Online (Sandbox Code Playgroud)
我已经收窄至类型参数<T>的myMethod; 如果我删除它并使用Object参数类型代码编译.即使我没有使用type参数,也声明myMethod为static <T> void myMethod() { }失败(仅限9).
我检查了Java 9发行说明并搜索了此行为的解释但没有找到任何内容.我是否正确地认为这是JDK9中的错误,或者我错过了什么?
Oracle已将此作为ID JDK-8191290的错误接受:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8191290
更新:错误已关闭,注释"固定在10,不会后退".
| 归档时间: |
|
| 查看次数: |
566 次 |
| 最近记录: |