JEP325: Switch expressions spec

John Rose john.r.rose at oracle.com
Thu May 10 02:20:26 UTC 2018


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-observers mailing list