JEP325: Switch expressions spec

Brian Goetz brian.goetz at oracle.com
Thu May 10 14:11:48 UTC 2018


This was the thinking behind UA1; that sometimes you were going to want 
to "fall into" an arrow case, and one of the reasons that UA1 allowed 
mixing:

     case B:
     case L -> S;

But, people hated that, so we went another way.

As John points out, the one place where it gets ugly is "case null, 
default".  Which we don't have to deal with yet, now that we've pushed 
case null onto the next switch bus.

On 5/9/2018 10:43 PM, Guy Steele wrote:
> I completely agree with John’s careful analysis, including the more speculative parts about symbol connotations.
>
> I too find "case B -> case L -> S” distasteful.  But on the other hand I also find ”case B: case L: S” distasteful.
>
> What I find acceptable is either
>
> 	case B ->
> 	case L -> S
>
> or
>
> 	case B:
> 	case L: S
>
> As a matter of style I strongly prefer the keyword “case” to appear only at the start of a line.
> This makes them easier to spot when reading code.
>
> What I am arguing for is to have a choice: when you want to put more than one pattern on a line, by all means use the keyword “case” just once and separate the patterns with commas.  But if you want to string them out vertically, I would like to have the option of repeating the “case” keyword once per line, whether using colons or arrows.
>
> And I argue that the latter style leads to more readable code in the “case null / default” situation; I prefer
>
> 	case null ->
> 	default -> E
>
> to
>
> 	case null, default -> E
>
> because I don’t like to bury that “default” keyword—I think it merits the same start-of-line visibility as the “case” keyword.  In addition, it really bugs me to treat “default” as if it were syntactically a pattern—it isn’t.
>
> However, if others prefer "case null, default -> E”, I would be okay with supporting that also.   But I would not want to write it.
>
> There is a tension or tradeoff between (a) the difference in connotations of arrow and colon, and (b) the simplicity of the theory that in principle they are more or less interchangeable syntactically, and we should make conversion from one to the other as easy as possible.  I may value (b) more than other people do.
>
> And now, having laid out my complete case (so to speak), I think I am done with arguing this, and am perfectly prepared to be outvoted or overruled.
>
> —Guy
>
>
>> On May 9, 2018, at 10:20 PM, John Rose <john.r.rose at oracle.com> wrote:
>>
>> A quick $0.02 on one bit of the switch syntax-alooza:
>>
>> I tried hard to like Guy's theory that -> and : are two flavors
>> of the same SwitchLabel syntax.  It is the most economical
>> way to spread the good parts from colonform to arrowform,
>> including the need (on occasion) to label one of the switch
>> clauses as the "default", even while it *also* has a "case N".
>> But I agree with others who find it too strange looking.
>>
>> If we can't buy Guy's clean syntax idea, we *do* need an
>> ad hoc way to combine multiple cases in arrowform and
>> an *even more* ad hoc way to fold in "default".
>> And of all the possibilities, I think Brian's (below) is
>> the least surprising and most acceptable.  Please,
>> let's not drop the ball and damage arrowform clauses
>> by forbidding multiple ClauseLabel inputs for them.
>>
>> Here's my take on formalizing Brian's suggestion:
>>
>> CasePattern:
>>   case Pattern { , Pattern } [ , default ]
>>   default
>>
>> (Note that default comes last in the CasePattern, no
>> matter what.)
>>
>> That's $0.0199.  The rest of it is why I think I can't like
>> Guy's proposal.  The colonform and arrowform just look
>> too different; colons and arrows have drastically different
>> connotations.  Arrow says "go from here to there" (e.g.,
>> from lambda parameter to lambda result).  Colon says,
>> "I'm telling you something about the following thing."
>> Chained arrows seem to say something like "I'm going
>> to breakfast, then to lunch, then to supper", not
>> "red and blue are the colors of this shoe", as colons
>> would.
>>
>> I know that's really subjective, but I think *something* like
>> that, some folk model or perception, is behind people's
>> distaste for "case B -> case L -> S" when the same people
>> are content with "case R: case B: S".  It's not that arrow
>> is heavier than colon, exactly; it's that arrow really means
>> something different than colon.
>>
>> At least, arrow and colon differ in the context of Java.
>> In some parts of some languages "a:b" does mean
>> "from a to b".  But in those places "a:b:c" doesn't mean
>> "from a or b to c" as in Java.  In any case, "a -> b -> c"
>> never means "from either a or b to c", as Guy's syntactic
>> deductions would lead us to try in Java.
>>
>> — John
>>
>> On Apr 23, 2018, at 11:20 AM, Guy Steele <guy.steele at oracle.com> wrote:
>>> I argue that there is no need to make the special-case exception for `default`.  When you need to play that game (usually because null needs to be addressed), you cannot use the comma-separation syntax.  Instead, just say either
>>>
>>> 	case null: default: s;			(or, if you prefer, `default: case null: s;`)
>>>
>>> or
>>>
>>> 	case null -> default -> s;		(or, if you prefer, `default -> case null -> s;`)
>> On Apr 20, 2018, at 11:40 AM, Brian Goetz <Brian.Goetz at Oracle.COM> wrote:
>>> One thing that is relevant to the short term is that now that we killed mixed labels, we'd have to have a way to say "case null or default" in arrow world.  The least stupid thing seems to be to allow default to be tacked on to a comma-separated case list as if it were a pattern:
>>>
>>>     case A -> s1;
>>>     case null, default -> s2;
>>>
>>> since you can no longer say:
>>>
>>>     case A -> s1;
>>>     case null:
>>>     default:
>>>         s2;
>>>
>>



More information about the amber-spec-experts mailing list