On 14 June 2018 at 15:48, Sara Golemon <poll...@php.net> wrote:

> Agree that it's more likely to be all-or-not within a switch block.
> If I could step through my thinking in putting it on the case
> statement however, I applied two starting rules:
> 1. Avoid adding new reserved symbols/keywords.
> 2. Try to make it read naturally.
>
> I actually did consider Rowan's suggestion `switch (expr) use (===)
> {}`, but I didn't feel like it satisfied #2 well.
>


I always read it in my head as "switch on expression using the ===
operator". There is a bit of a punctuation pileup, though.




> I'll be honest, I didn't realize the fall-through behavior was a
> learning curve issue.  This block scoping would address that, but I
> wonder if we could make use of break/continue.  The former's meaning
> being obvious enough, the latter allowing us to either fall through to
> the next case or possibly continuing evaluating conditionals.


Yeah, annoyingly "continue" is actually valid in switch statements, as a
confusing synonym of "break", because I always thought it would be nice if
you had to balance every non-empty case with either "break" or "continue".
It's not just the learning curve that's a problem, it's really easy to
write subtly broken code with an accidental fall-through.

I was just telling a colleague that I've made this mistake before:

//Before: no break needed because there's a return
switch ( $foo ) {
    case 1:
       return true;
   default:
       bar();
}

//After: should have added a break, but didn't notice until something went
wrong
switch ( $foo ) {
    case 1:
       if ( $something ) {
            return true;
       }
   default:
       bar();
}

I think a lot of people would find a syntax with { ... } blocks much more
familiar.

One thing to consider is what actually is the "mission statement" of the
switch/match/whatever - what makes it "better" than a chain of if-elseif
blocks?


Meanwhile, someone just posted this on Stack Overflow
https://stackoverflow.com/q/50860319/157957 where someone was trying to use
<= in a switch statement!


Regards,
-- 
Rowan Collins
[IMSoP]

Reply via email to