5 ms·
Adding clarity to switch statements: a humble PHP feature proposal
- DanHulton 16y agoThe 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 16y agoI would really love for it to be an error to include neither, but that would be a massively backwards-incompatible change.
- markchristian 16y agoSure, but it could raise an E_STRICT.
- ars 16y agoThat would still be a massively backwards-incompatible change. Lots of people run using E_STRICT.
- stackthat 16y agoswitch 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 16y agoDoes VB have a switch statement, or do you mean select? (Not nitpicking, just curious. It's been a while since I used it.)
- stackthat 16y agoI mean select
- stackthat 16y agoBetter solution would be getting rid of "break" and breaking by default like VB but no one would like that...
- markchristian 16y agoActually, that's not a better solution -- case fall through is remarkably handy. It's just a problem if it happens by accident.
- jrockway 16y agoI 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 16y agoThat's not bad. I take it you you could have "when(foo or bar)" to handle multiple cases?
- chromatic 16y agogiven/when are better than that; see http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detai... for all of the possibilities.
- sofuture 16y agoI 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?!
- wccrawford 16y agoI'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 16y agoAs 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.
- TW0 16y agoswitch($some_value) { case 1 : doSomething(); case 2 : doSomething(); break; case 3 : doSomethingElse(); break; default : doSomething(); doSomethingElse(); }
- 87159377 16y agoswitch($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.