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.
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;
}))(),
...
};
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.See: https://docs.oracle.com/en/java/javase/14/language/switch-expressions.html#GUID-BA4F63E3-4823-43C6-A5F3-BAA4A2EF3ADC__GUID-4900EB1C-3832-4CB8-ACAE-A87675260B75
Example:
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 usevar
).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 ofyield
.For example: