5 ms·
Fixing switch - what I want out of switch statements
- bendauphinee 14y agoInteresting idea. I can think of a few places in my code this would be useful.
- Ralith 14y agoYour 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?
- ggchappell 14y agoSL: 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 14y agoYeah, 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.