1

Given the following code:

public class Foo {

    public static void main(String[] args) {
//      List<Object> list = getFooList();   // case A
        List list = getFooList();           // case B
    
        boolean b = list.stream().map(Foo::toFoo).allMatch(Foo::is);
    }

    public static List getFooList() {
        return List.of(new Foo());
    }

    private static Foo toFoo(Object o) {
        return (Foo)o;
    }

    public static boolean is(Foo f) {
        return true;
    }

}

Case A works fine. Case B fails because map returns a raw stream and is(Object) does not exist

This is surprising to me because i would assume that map(Foo::toFoo) would return a Stream<Foo> because toFoo() returns a Foo.

Why is the compiler not able to determine the type in this case?

I use Java 11 and the Eclipse compiler

Dieter
  • 120
  • 7

0 Answers0