A DESCRIPTION OF THE PROBLEM :
Compilation fails if yield statement is followed by underscore, even if it is legal syntax, like yield _ -> {}. This is issue that has been present in jdk since introduction of unnamed variables and patterns
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile following code:
public class SwitchYieldAndUnderscore {
public static void main(String[] args) {
Consumer<String> value = switch (args[0]) {
case "foo" -> {
yield _ -> {};
}
default -> throw new AssertionError();
};
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation successful
ACTUAL -
Compilation failes with 2 errors:
";" exprected
"=" exprected
---------- BEGIN SOURCE ----------
public class SwitchYieldAndUnderscore {
public static void main(String[] args) {
Consumer<String> value = switch (args[0]) {
case "foo" -> {
yield _ -> {};
}
default -> throw new AssertionError();
};
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Enclose _ in paretheses like so: yield (_) -> {}
FREQUENCY : always