Switch Statement Fall-Through
Brian Goetz
brian.goetz at oracle.com
Tue Nov 5 15:23:11 UTC 2019
I understand your point. But I think you are looking at it backwards. It is a popular misconception that Fall Thru is inherently bad. But it’s not, and sometimes it’s even necessary. What was the mistake was fall through by default. But we can’t fix that mistake now.
If because of the nature of your problem, you need fall through, you should use the colon form. And now you can do so either for statements or expressions, with no new surprising interactions. If you do not need Fall Thru you should probably use the arrow form. And again you can do so either for statements or expressions.
So I would argue that the issue in your example is not the fault of the language but that the author chose the wrong construct for this situation. But that doesn’t mean there aren’t situations where fall through is necessary. We chose to design this feature update without biasing towards or away from this. I can understand how others might choose a more opinionated language design, or want to prioritize fixing what they perceive as a mistake of the past, but that wasn’t the goal.
I think it’s also important to recognize that no matter what we do we are grappling with the mistakes of Legacy. We can move the pain around, but we are still stuck with the choices of 1995. We had a choice between extending the construct within its original spirit, or building something very different right next door to it, knowing we would not be able to get rid of the old one. And we chose the former. I think it was the right trade off, but I have no illusion that everybody will like it.
Sent from my iPad
> On Nov 5, 2019, at 3:37 PM, August Nagro <augustnagro at gmail.com> wrote:
>
> 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