Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) in some specific situation

Przemysław R&#243;życki picture Przemysław Różycki · Feb 16, 2017 · Viewed 28.8k times · Source

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).

Answer

Przemysław R&#243;życki picture Przemysław Różycki · Mar 13, 2017

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.