【发布时间】:2017-07-06 06:25:44
【问题描述】:
大家早上好,
我一直在努力理解为什么以下代码不能在 Eclipse Neon (JDT 3.12.3.v20170301-0400) 中编译,但可以使用 javac 或 Eclipse Mars 完美编译:
public class TestLambda {
protected static <K, V> Map<K, V> newMap(final Function<K, V> loader) {
return new HashMap<>();
}
private final Map<Integer, Integer> working = newMap(key -> {
final List<String> strings = new ArrayList<>();
final String[] array = strings.toArray(new String[strings.size()]);
foo(array);
return null;
});
private final Map<Void, Void> notWorking = newMap(key -> {
final List<String> strings = new ArrayList<>();
// This line seems to be the root of all evils
foo(strings.toArray(new String[strings.size()]));
return null;
});
private void foo(final String[] x) {}
private void foo(final Integer[] x) {}
}
Eclipse 编译器显示“类型不匹配:无法从 Map<Object,Object> 转换为 Map<Void,Void>”。
它似乎无法知道必须调用哪个 foo 方法...
我做错了吗?
提前感谢您的帮助
【问题讨论】:
-
我很困惑:错误发生在哪一行?您突出显示的那个根本不使用地图。
foo(Integer[])是干什么用的? -
我知道,有问题的代码中没有使用 Map。只需在 Eclipse Neon 中加载这段代码,看看会发生什么......
-
这段实际的代码并没有做任何有用的事情,甚至根本没有任何意义......它只是其他代码的精简版本,试图说明和隔离有问题的行为
-
您是说
foo(Integer [])重载必须存在才能显示此行为?如果没有,你也应该把它去掉。 (我在手机自动取款机上)。 -
是的,它必须在那里......
标签: java eclipse generics lambda eclipse-neon