无法推断 <R> 的类型参数map(Function<? super T,? extends R>) 在某些特定情况下 [英] Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) in some specific situation
问题描述
我在 Sandbox.java 文件中有以下类:
I have the following classes in a file 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);
}
}
Eclipse 中的编译在第 14 行显示错误无法推断 map(Function) 的类型参数".
the compilation in Eclipse shows error in line 14 "Cannot infer type argument(s) for map(Function) ".
使用纯 javac (JDK 1.8.0_121) 编译相同的代码没有问题.
The same code compiles without problems using pure 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());
然后代码在 Eclipse 中编译没有错误.
then the code compiles without error in Eclipse.
有谁知道为什么会有这种行为?是bug吗?
Does anyone know why is there such a behaviour? Is it a bug?
我使用 Eclipse 4.6.2.20161208-0625(目前没有发现更新).
I use Eclipse 4.6.2.20161208-0625 (it finds no updates at the moment).
推荐答案
我已经确认,这是一个错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=512486.它已在 4.6.3 中声明为已解决.当稳定版本可用时,我会确认这一点.
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.
这篇关于无法推断 <R> 的类型参数map(Function<? super T,? extends R>) 在某些特定情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!