无法推断< R>的类型参数。在某些特定情况下的map(Function&lt ;? super T,?extended R>) [英] Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) in some specific situation

查看:514
本文介绍了无法推断< R>的类型参数。在某些特定情况下的map(Function&lt ;? super T,?extended R>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件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行显示错误不能

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.

有人知道为什么会有这种行为吗?

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.

这篇关于无法推断&lt; R&gt;的类型参数。在某些特定情况下的map(Function&lt ;? super T,?extended R&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆