java - 在某些特定情况下,无法为 <R> map(Function<? super T,? extends R>) 推断类型参数

标签 java eclipse compiler-errors eclipse-neon

我在文件 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) 的类型参数”。

使用纯 javac (JDK 1.8.0_121) 可以毫无问题地编译相同的代码。

如果我将正确的行更改为:

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 中编译没有错误。

有谁知道为什么会有这样的行为?是错误吗?

我使用 Eclipse 4.6.2.20161208-0625(目前没有发现更新)。

最佳答案

我已经确认,这是一个错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=512486 .它已在 4.6.3 中声明为已解决。当稳定版本可用时,我会确认这一点。

关于java - 在某些特定情况下,无法为 <R> map(Function<? super T,? extends R>) 推断类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285370/

相关文章:

java - 用像 Collection<?> 这样的通配符替换像 Collection 这样的原始类型是否向后兼容?

java - 如何在Java中每第n次出现字符时分割字符串

java - 我的 bin 文件夹中出现奇怪的类文件

c - 仅当参数不是常量时,math.h 中的 sqrt 才会导致链接器错误 "undefined reference to sqrt"

java - Android studio-transformClassesWithDexForDebug 错误

java android View 结构

java - Google App Engine Java,无法访问本地主机!

c - 如何在不影响性能的情况下实现原始类型的类型安全?

c - 我缺少什么,所以我可以编译我的源代码?我是新手

java - 隐藏进程命令行