Expression switch - an alternate proposal

forax at forax at
Tue Apr 10 08:02:24 UTC 2018

----- Mail original -----
> De: "Brian Goetz" <brian.goetz at>
> À: "Remi Forax" <forax at>, "Stephen Colebourne" <scolebourne at>
> Cc: "amber-spec-experts" <amber-spec-experts at>
> Envoyé: Lundi 9 Avril 2018 17:03:12
> Objet: Re: Expression switch - an alternate proposal

>> I think i agree with you about the fact that the expression switch does need to
>> support fallthrough,
>> more on that in a folowing email.
> I've been leaving this topic until we have ironed out the higher-order
> bits, but this seems a good enough time to start this discussion.
>> I also agree with you that mixing arrows and colons is confusing.
> I agree this is confusing, but I think it is also not likely to be
> something people do naturally -- because the -> form, where it is
> applicable, is so much more attractive -- so the risk of confusion is
> low.   Just as style guides say to users "if you're going to use fall
> through, label it clearly", and most code does, style guides will guide
> users away from this confusion.
>> Basically, your proposal is to use -> eveywhere, i think i prefer the opposite,
>> do not use arrow at all.
>> Using arrow in this context is disturbing because it doesn't mean the same
>> things if it's the arrow of the lambda or the arrow inside an expression
>> switch.
> This is a reasonable alternative, but I don't think it would be very
> popular.  I think people will really love being able to write:
>     case MONDAY -> 1;
>     case TUESDAY -> 2;
> and will be sad if we make them write
>     case MONDAY: break 1;
>     case TUESDAY: break 2;
> Not only will they be said, but they will point out that the "obvious"
> answer was in front of our noses, and we did something different just to
> be different.  (You can easily imagine the "There those Java guys go
> again, verbosity for its own sake" rants, but this time they might
> actually be right, rather than the folks who can't spell "migration
> compatibility" complaining about erasure.)

Apart from the semantics difference between -> inside a lambda and -> inside a case,
the fact that you can use -> but not -> { } let me think that if we need a shorter syntax,
a one that use -> is not the best one.

>> the problem is that currently -> means create a new function scope and not
>> creates a new code scope.
> I think the scopes issue is a red herring.
>> So if do not mixing arrows and colons is an important goal and i think it is, i
>> think it's better to not use arrow.
> Or just: avoid mixing arrows and colons.

That's may be hard, if you take ASM code as an example, we have two kind of switchs, low level ones to parse method descriptor, generics signature, etc that will continue to use the statement descriptor and "association" switch, that associate a value to another value, when ASM transforms the high level Visitor API to low level bytecodes or when ASM does abstract analysis like computing the stack frames, those can be transformed to expression switch but if you take a look to these switch, usually there is do computation/allocations so written as an expression switch, there will be case with one single expression (most of them) but also one or two cases by switch that will assign a local variable, so with the current proposed syntax, it means mixing arrows and colons. 

I'm sure there are other shorter syntax possible that does not use ->, technically we do even need the symbol ->, so why not just use ':' as a shorter syntax.
You may think that it means that the grammar as to be smarter to distinguish between a single expression and a statement that may be followed by other statements but you can parse everything as statements and in a later phase if there is only one expression consider it as a break expression.

The main drawback i see by not having to use '->' in the grammar is that you can not allow fallthrough but i think we should disable fallthrough in an expression switch anyway.
So in term of design, i see it in the opposite way, the fact that we do not allow fallthrough allow us more degree of freedom in term of syntax so let us use a more regular syntax by avoiding to introduce '->'.

I think not introducing -> as also the nice effect of making the expression switch less alien compared to the statement switch because it remove one of the syntactic difference between them.

>> Moreover, do we really need a shorter syntax given that we can use break and a
>> value ?
> I suggest you do this poll at Devoxx.  Make sure to wear flame-proof pants!

I have a 3 hours session with José Paumard at Devoxx France (only 3000 attendees, so a little smaller than the real Devoxx in Belgium) next week on amber and valhalla.
So i will run the poll, we will see. For the pants, i've a plan :)

>> and now we can discuss about adding a shorter syntax by making break optional if
>> there is one expression.
> ... which we expect to be true almost all the time.


More information about the amber-spec-observers mailing list