-
-
Notifications
You must be signed in to change notification settings - Fork 105
Open
Labels
Description
With #63, basic support for Java 14 switch expressions was added. There is a multi-statement form of switch expressions that is not yet supported, using the yield keyword to yield the expression from the block of statements.
Example:
Day day = Day.WEDNESDAY;
int numLetters = switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
yield 6;
case TUESDAY:
System.out.println(7);
yield 7;
case THURSDAY:
case SATURDAY:
System.out.println(8);
yield 8;
case WEDNESDAY:
System.out.println(9);
yield 9;
default:
throw new IllegalStateException("Invalid day: " + day);
};
System.out.println(numLetters);We might be able to half-translate this into IIFE-style lambda functions, with a Func<SPECIFY_ME> type where the user must replace SPECIFY_ME with whatever the appropriate type is (which might not be obvious syntactically i.e. if you use var).
This is ugly but it might work in most cases. A warning should be emitted whenever we have to do this. Also note return from the lambda instead of yield.
For example:
Day day = Day.WEDNESDAY;
int numLetters = day switch {
MONDAY or FRIDAY or SUNDAY => ((Func<SPECIFY_ME>)(() =>
{
Console.WriteLine(6);
return 6;
}))(),
...
};ziaulhasanhamim