JEP325: Switch expressions spec
Guy Steele
guy.steele at oracle.com
Mon Apr 23 19:07:41 UTC 2018
I agree; and that is exactly why I would throw in a newline in situations where I cared to use two arrows:
case pat1 ->
case pat2 -> s1;
is much to be preferred to
case pat1 -> case pat2 -> s1;
Look, I also think that it’s bad form to write
a = b ? c : d;
because of the risk of misinterpretation; I would write
a = (b ? c : d);
I don’t think the risks of switch syntax are worse (or better) than this.
> On Apr 23, 2018, at 3:07 PM, forax at univ-mlv.fr wrote:
>
> '->' being a two characters symbol (at least if you do not enable font ligature of your IDE/editor) is a more strong separator than comma ',',
> so i think it's easy to visually parse
> case pat1, pat2 -> s
> as
> case [pat1, pat2] -> s
> than
> case pat1, [pat2 -> s]
>
> Rémi
>
> ----- Mail original -----
>> De: "Guy Steele" <guy.steele at oracle.com>
>> À: "Remi Forax" <forax at univ-mlv.fr>
>> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
>> Envoyé: Lundi 23 Avril 2018 20:32:26
>> Objet: Re: JEP325: Switch expressions spec
>
>>> On Apr 23, 2018, at 2:27 PM, Guy Steele <guy.steele at oracle.com> wrote:
>>>
>>> Good point, Rémi. However, note that
>>>
>>> case pat1, pat2 -> s
>>>
>>> is equally too close to
>>>
>>> case pat1 -> pat2 -> s
>>>
>>> and again they have very different meanings.
>>>
>>> We have to admit that there is room to blunder with this syntax.
>>>
>>> One way out would be to use a different arrow for `switch` statements:
>>>
>>> switch (x) {
>>> case pat1 => case pat2 => s1;
>>> case pat3 => pat4 -> s2;
>>> case pat5, pat6 => s2;
>>> case pat7, pat8 => pat9 -> s4;
>>> }
>>
>> As a careful coder, if I did not have a separate arrow `=>` (and probably even
>> if I did), I would use formatting and parentheses to convey my intent:
>>
>> switch (x) {
>> case pat1 ->
>> case pat2 -> s1;
>> case pat3 -> (pat4 -> s2);
>> case pat5, pat6 -> s2;
>> case pat7, pat8 -> (pat9 -> s4);
>> }
>>
>> —Guy
More information about the amber-spec-experts
mailing list