

Adding clarity to switch statements: a humble PHP feature proposal - markchristian
http://shinyplasticbag.posterous.com/php-feature-proposal

======
sofuture
I don't see what this provides aside from redundant verbosity... How much a
stumbling block is switch really?

"Oh you mean it just keeps going until you break? Okay, duly noted."

Fall through switch is quite useful, and if you really wanted an if-else,
you'd use one, right?!

------
jrockway
I like what Perl does: it separates cases by making them look like regular
blocks:

    
    
        given($cat_macro){
            when(/OH HAI/){ 
                say "ceiling cat is watching you switch statements";
            }
            when(/OH NOES/){
                die "FAIL";
            }
            default { 
                say "<picture of a walrus with a bucket>";
            }
        }
    

Pretty easy to follow the control flow, even when they aren't oneliners.

~~~
markchristian
That's not bad. I take it you you could have "when(foo or bar)" to handle
multiple cases?

~~~
chromatic
given/when are better than that; see
[http://perldoc.perl.org/perlsyn.html#Smart-matching-in-
detai...](http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail) for
all of the possibilities.

------
wccrawford
I've got this great idea. Instead of :

next;

have:

// Continue through next case

The best part is that it's built into the language!!

~~~
markchristian
As mentioned in the entry, the idea of having an actual keyword is a bit
better than that. In particular, it would make it feasible for a strict notice
to be raised if neither keyword was used.

------
DanHulton
The example implementation loses out on what actually putting it in the
language would gain, though: causing an error when forgetting to include next;
or break;.

~~~
seldo
I would really love for it to be an error to include neither, but that would
be a massively backwards-incompatible change.

~~~
markchristian
Sure, but it could raise an E_STRICT.

~~~
ars
That would still be a massively backwards-incompatible change. Lots of people
run using E_STRICT.

------
stackthat
Better solution would be getting rid of "break" and breaking by default like
VB but no one would like that...

~~~
markchristian
Actually, that's not a better solution -- case fall through is remarkably
handy. It's just a problem if it happens by accident.

------
stackthat
switch in C syntax is terrible implemented, VB done this one so much better.
That's one of the reasons why switch is not that popular in C languages
(people tend to do crazy if-then-else stuff) and so popular in VB, most devs
tries to use select where possible.

~~~
markchristian
Does VB have a switch statement, or do you mean select? (Not nitpicking, just
curious. It's been a while since I used it.)

~~~
stackthat
I mean select

------
TW0
switch($some_value) { case 1 : doSomething(); case 2 : doSomething(); break;
case 3 : doSomethingElse(); break; default : doSomething(); doSomethingElse();
}

------
87159377
switch($some_value) { case 1 : doSomething(); case 2 : doSomething(); break;
case 3 : doSomethingElse(); break; default : doSomething(); doSomethingElse();
}

Is there any reason why keeping things simple is not an option? The number of
possible outcomes in a switch case can often be so many that falling through
would just be the route to complicated and difficult to manage code.

