1

I'm surprised, why did the lambda expression can't be invoked at runtime correctly. is it a type inference error? The work aournd example is uncomment the explicit type argument declaration.

public class LambdaConversionTest {
    @Test
    public void whyDidLambdaExpressionFailsWithLambdaConversionException() {
        try {
            //      v--- when uncomment it, the behavior is what I expected
            Stream./*<Collector>*/of(new Robot(), new Puller())
                  .flatMapToInt(Collector::stream).sum();
            fail("fails with expected behavior");
        } catch (BootstrapMethodError why) {
            assertThat(why.getCause().getMessage()
                 ,containsString("Invalid receiver type interface "));
        }
    }

    interface Marker {
    }

    interface Collector {
        IntStream stream();
    }

    class Robot implements Collector, Marker {
        @Override
        public IntStream stream() {
            return IntStream.empty();
        }
    }

    class Puller implements Collector, Marker {
        @Override
        public IntStream stream() {
            return IntStream.empty();
        }
    }
}
1
  • @Holger sir, thank you mark the question as duplicated. good job. I find it for a while but I can't find the sufficient answer. Commented Feb 28, 2018 at 14:18

1 Answer 1

2

For this particular instance of this JDK bug, it should be noted that it does not fail if you declare the interface Marker after Collector

Fail:

interface Marker {}

interface Collector {
    IntStream stream();
}

Pass:

interface Collector {
    IntStream stream();
}
interface Marker {}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your explaination!
For this specific example, it’s unspecified whether the compiler should infer Stream<Marker&Collector> or Stream<Collector&Marker> for the stream type, so it’s not wrong if this decision depends on the order of the interface declarations, as it should not matter anyway, but of course, if one of the two possibilities triggers the bug, it does make a difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.