问题
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);
}
}
the compilation in Eclipse shows error in line 14 "Cannot infer type argument(s) for map(Function) ".
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());
then the code compiles without error in 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).
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/42285370/cannot-infer-type-arguments-for-r-mapfunction-super-t-extends-r-in-so