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

36,347

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

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

Solution 3

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

Share:
36,347
Przemysław Różycki
Author by

Przemysław Różycki

IT Architect with experience in Java technologies

Updated on July 20, 2022

Comments

  • Przemysław Różycki
    Przemysław Różycki almost 2 years

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