Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile time: no instance(s) of type variable(s) U exist

The following statement, although nonsensical, appears syntactically sound.

final Stream<LongStream> foobar = IntStream.empty()
    .flatMap(x -> IntStream.empty()
        .mapToObj(y -> IntStream.empty()
            .mapToLong(z -> 1))); //compilation error here on `z -> 1`

However it does not compile, returning:

java: incompatible types: bad return type in lambda expression no instance(s) of type variable(s) U exist so that java.util.stream.Stream conforms to java.util.stream.IntStream

However if you delay the flatmap, everything works fine:

final Stream<LongStream> foobar = IntStream.empty()
    .mapToObj(x -> IntStream.empty()
        .mapToObj(y -> IntStream.empty()
            .mapToLong(z -> 1)))
    .flatMap(x -> x);

What is the difference between .mapToObj(..).flatMap(..) and just .flatMap(..)? Is there someway to eliminate the extra flatmap call?

like image 922
Andreas Avatar asked Mar 15 '17 05:03

Andreas


2 Answers

.mapToObj(..).flatMap(..) and .flatMap(..) expect completely different signatures.

.mapToObj(..).flatMap(..) expects an int -> Object function, and an Object -> Stream<?> function.

.flatMap(..) expects an int -> IntStream function.

If you break down your code, you're passing an int -> Stream<LongStream> function, which isn't compatible with an int -> IntStream function.

You would have the same error with this simplified code:

IntStream.empty().flatMap(x -> Stream.of(LongStream.empty()));
like image 50
4castle Avatar answered Oct 29 '22 06:10

4castle


I've refactored your method to break down what it's doing:

IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1);
final Stream<LongStream> foobar = IntStream.empty().flatMap(f2);

We have two things wrong here:

The lambda on line 2 does not return a LongStream, but rather a Stream<LongStream>, as we are converting each int in our stream to a LongStream. If you intend for it to be a single LongStream, you need to do a flatMapToLong.

The flatMap on line 3 expects an int -> int function, which yours is not. However, you can use mapToObj instead, which takes the method that you're providing it.

So the corrected method would be:

IntFunction<LongStream> f1 = y -> IntStream.empty().mapToLong(z -> 1);
IntFunction<LongStream> f2 = x -> IntStream.empty().mapToObj(f1).flatMapToLong(i -> i);
final Stream<LongStream> foobar = IntStream.empty().mapToObj(f2);
like image 1
Joe C Avatar answered Oct 29 '22 06:10

Joe C