Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 757
Przemysław Różycki Avatar asked Feb 16 '17 21:02

Przemysław Różycki


3 Answers

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.

like image 60
Przemysław Różycki Avatar answered Nov 12 '22 09:11

Przemysław Różycki


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.

like image 43
Pallavi Sonal Avatar answered Nov 12 '22 07:11

Pallavi Sonal


I got this error when one of the parameters was of the wrong type. Correcting that made the error go away

like image 1
Gayathri Avatar answered Nov 12 '22 09:11

Gayathri