Switch Statement Fall-Through
August Nagro
augustnagro at gmail.com
Tue Nov 5 14:37:53 UTC 2019
Hi Brian, thanks for the response.
I actually like the old switch. My problem with the new design is readability. Consider a new Java developer excited to use switch / pattern matching that writes:
var instruction = switch (trafficLight) {
case RED: yield "Stop";
case YELLOW:
System.out.println("Prepare");
case GREEN: yield "Go";
};
and/or
switch (trafficLight) {
case RED -> doStop();
case GO -> doGo();
}
Which one is fall-through? Which is exhaustive? Because the switch statement’s properties depend on context, people will be confused.
When I see an arrow-style (what I meant by saying new-style in my first email) switch, I’d like it to not fall through and guarantee exhaustiveness in all cases. Otherwise, I can just use the classic switch!
> On Nov 5, 2019, at 2:46 AM, Brian Goetz <brian.goetz at oracle.com> wrote:
>
> I think what is tripping you up is the idea that this is somehow “new switch”. In fact, quite the opposite is true — this is old switch, with some upgrades to smooth out some wrinkles.
>
> In that sense, saying “new switch falling through” is a syntax error — it is the familiar old switch that falls through, today as yesterday. Nothing has changed there.
>
> (I totally understand why one would want to say “old switch stinks, let’s make a new one”. Which is the essence of Stephens argument. What we chose to do instead is rehabilitate the flawed switch we have, with full awareness that it’s flaws are here to stay, rather than create a (likely differently flawed) companion construct, which is almost like, but not quite like, an old flawed construct we still won’t be able to get rid of.)
>
> By having multiple axes of variation between the forms, Your suggestion essentially creates a new and old switch distinction, just with the same name. And I get why you would find that appealing; you would like to relegate the old switch to the scrap heap of legacy. But in terms of the language as a whole, it’s still two similar-but-oddly-different choices with the same name. I think that would be equally confusing to new users — “why are there two kinds of switch they are different in multiple ways”. Which is exactly why we didn’t do it this way.
>
>
> Sent from my MacBook Wheel
>
>> On Nov 5, 2019, at 12:19 AM, August Nagro <augustnagro at gmail.com> wrote:
>>
>> Hi!
>>
>> I recently read Stephen Colebourne’s blog on the new switch syntax (https://blog.joda.org/2019/11/java-switch-4-wrongs-dont-make-right.html), and although I don’t agree with the proposed solution, his issue with the new switch falling-through is spot-on.
>>
>> I've personally used fall-through switch statements, but I think they should only ever be applicable to the old-style switch. Having the compiler enforce this will prevent a lot of misuse and confusion. Instead of four types of switch, there is only two:
>>
>> 1. switch with only ->, that doesn't fall-through, checks exhaustiveness, has yield, commas between cases, and can be used as an expression.
>> 2. switch with only :, the standard switch.
>>
>> Sincerely,
>>
>> August Nagro
>
More information about the amber-dev
mailing list