这段代码:
public static void f(String[] args) {}
public static void f(Integer[] args) {}
public static void main(String[] args)
{
f(Stream.of("xxx").toArray(i -> new String[i]));
}
Run Code Online (Sandbox Code Playgroud)
使用jdk8u45编译成功但jdk8u60会输出以下错误:
Error:(17, 9) java: reference to f is ambiguous
both method f(java.lang.String[]) in type_infer.Test and method f(java.lang.Integer[]) in type_infer.Test match
Run Code Online (Sandbox Code Playgroud)
它是否遵循JSL,为什么编译器无法解析重载方法?这是jdk8u45中的固定错误吗?
这看起来像是最近在javac.
调用中的参数f(..)显然是类型String[]。例如,这可以通过将相同的表达式解析为独立表达式来显示:
String s1 = Stream.of("xxx").toArray(i -> new String[i])[0];
String[] s2 = Stream.of("xxx").toArray(i -> new String[i]).clone();
Run Code Online (Sandbox Code Playgroud)
所有版本的编译器都接受这些。
显然,f(Integer[])不适用于 类型的参数String[]。我看不出为什么外部类型推断会迫使内部解析得到Object[]比作为独立表达式的解析更糟糕的结果(例如,)。
附加证据:即使javac受此错误影响的那些版本也可以正确拒绝以下语句:
Integer[] s3 = Stream.of("xxx").toArray(i -> new String[i]);
Run Code Online (Sandbox Code Playgroud)
因此f(Integer[])显然是无法计算的。
| 归档时间: |
|
| 查看次数: |
849 次 |
| 最近记录: |