eclipse 在某些特定情况下无法推断 <R> map(Function<? super T,? extends R>) 的类型参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42285370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) in some specific situation
提问by Przemys?aw Ró?ycki
I have the following classes in a file Sandbox.java:
我在文件 Sandbox.java 中有以下类:
package sandbox;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class Sandbox {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
executor.shutdown();
System.out.println(s);
}
}
class Wrapper<T> {
T t;
private Wrapper(T t) {
this.t = t;
}
public T get() {
return t;
}
public static <T> Wrapper<T> of (T t) {
return new Wrapper<>(t);
}
}
the compilation in Eclipse shows error in line 14 "Cannot infer type argument(s) for map(Function) ".
Eclipse 中的编译在第 14 行“无法推断 map(Function) 的类型参数”中显示错误。
The same code compiles without problems using pure javac (JDK 1.8.0_121).
使用纯 javac (JDK 1.8.0_121) 编译相同的代码没有问题。
If I change the proper line into:
如果我将正确的行更改为:
Collection<String> s = Arrays.asList(1,2,4,100).stream()
.map(i -> CompletableFuture
.supplyAsync(() -> Wrapper.of(i), executor)
.<String>thenApply(d -> d.get().toString())
)
.map(CompletableFuture::join)
.collect(Collectors.toList());
then the code compiles without error in Eclipse.
然后代码在 Eclipse 中编译没有错误。
Does anyone know why is there such a behaviour? Is it a bug?
有谁知道为什么会有这种行为?这是一个错误吗?
I use Eclipse 4.6.2.20161208-0625 (it finds no updates at the moment).
我使用 Eclipse 4.6.2.20161208-0625(它目前没有找到更新)。
采纳答案by Przemys?aw Ró?ycki
I have confirmed, that this is a bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=512486. It has been declared as resolved in 4.6.3. I'll confirm this when stable release is available.
我已经确认,这是一个错误:https: //bugs.eclipse.org/bugs/show_bug.cgi?id=512486。它已在 4.6.3 中声明为已解决。当稳定版本可用时,我会确认这一点。
回答by Pallavi Sonal
I just checked with Eclipse IDE for Java Developers Version: Mars Release (4.5.0) Build id: 20150621-1200 and the code worked well for me. It may have been introduced in the 4.6 version.
我刚刚检查了 Eclipse IDE for Java Developers Version: Mars Release (4.5.0) Build id: 20150621-1200 并且代码对我来说运行良好。它可能是在 4.6 版本中引入的。