New JEP: Switch Expressions for the Java Language

forax at forax at
Fri Dec 8 14:59:44 UTC 2017

----- Mail original -----
> De: "Brian Goetz" <brian.goetz at>
> À: "Remi Forax" <forax at>
> Cc: "amber-spec-experts" <amber-spec-experts at>
> Envoyé: Vendredi 8 Décembre 2017 15:48:36
> Objet: Re: New JEP: Switch Expressions for the Java Language

> Clearly accessing locals should be allowed, to some degree -- it would
> be weird if we could not reference y here.  But it is reasonable to ask,
> what restrictions do we want to place? 


> Are the same restrictions as lambdas -- effectively-final only -- too restrictive? 

I believe it's ok to have the same restrictions as lambdas.
We still have the classical switch as backup and given that the current proposed syntax uses '->', it will be weird if expression/body after the -> has another set of rules as the lambda ones.

> (Certainly such a restriction opens up more options for translation strategies, and is
> consistent with other -> contexts.)

yes, if the expression switch as the same restrictions as a lambda expression/body, it means we can represent each expression as a static method like with the lambda translation instead of having one blob of code for all expressions, so we can re-organise them, not even make one of them present if the case is never called, etc.


> On 12/7/2017 6:02 PM, Remi Forax wrote:
>> Hi Brian,
>> correct me of i'm wrong but there is no discussion about accessing to a local
>> variable in the body of an expression,
>> void m(int x, int y) {
>>    return switch(x) {
>>      case 0 -> y;   // is it allowed, how is it translated ?
>>      default -> 0;
>>    };
>> }
>> regards,
>> Rémi
>> ----- Mail original -----
>>> De: "Brian Goetz" <brian.goetz at>
>>> À: "amber-spec-experts" <amber-spec-experts at>
>>> Envoyé: Jeudi 7 Décembre 2017 23:33:36
>>> Objet: New JEP: Switch Expressions for the Java Language
>>> We've separated out a package of standalone improvements to `switch`
>>> (switch expressions, case null, and case alternation) into their own JEP:
> >>

More information about the amber-spec-experts mailing list