There is a bad interaction between a lambda and a switch expression,
a lambda allows its expression to be typed void but a switch expression can not
be typed void,
so the following code does not compile
sealed interface I permits A, B {}
record A() {}
record B() {}
public Optional<A> findOneA(List<I> list) {
return list.stream()
.<A>mapMulti((i, consumer) -> switch(i) {
case A a -> consumer.accept(a);
case B b -> {}
})
.findFirst();
}
This bug occurs for with any methods that takes a Consumer has parameter (so
stream.forEach/peek, Iterator.forEachRemaining etc).
The workaound is to add a pair of curly braces around the switch to transform
it to a switch statement.
For me, it should be possible to have a switch expression typed void. This is a
backward compatible change given that the code does not compile otherwise.
regards,
Rémi