

Fixing switch - what I want out of switch statements - samuellevy
http://www.samuellevy.com/blog/2012/07/fixing-switch-what-i-want-out-switch-statements

======
ggchappell
SL: What you want out of switch statements is an easy way to implement a state
machine -- or state-machine-like construct.

While this is a worthy goal, I don't think your proposed syntax is very
readable. It might be better to think a bit more abstractly, and instead of
asking, "How can we improve switch?" ask, "What features does a language need
in order to facilitate implementation of <whatever>?"

~~~
samuellevy
Yeah, it's only a brief crack at adding some syntax which would add some more
functionality to a known statement. In short, I've used switch many times and
thought to myself "Why can't I just tell it to start re-evaluating the cases
again?".

The example there isn't what you would expect to find in the real world
(instead it's a very cut-down piece of code to show how it would flow, and
show of the main points of my thinking). That being said, the switch statement
tends to be difficult to read in general, and is frequently misunderstood by
new coders. I remember that when I started learning to program, I just
expected it to work like this already, and was confused as to why I couldn't
just change the variable I was checking and trigger other cases.

------
Ralith
Your trivial example is already spaghetti code; I'm loathe to imagine what
real-world use would look like. It's also an internally inconsistent concept:
consider "case 3" and "case s > 4". Are you comparing values to the switch
statement, or testing conditions like an if statement? What do you do when
those two come into conflict?

------
bendauphinee
Interesting idea. I can think of a few places in my code this would be useful.

