4 ms·
Trying to compare a switch statement to proper pattern matching is like comparing a rock to a rocket.
by blueberry87 3y ago
Trying to compare a switch statement to proper pattern matching is like comparing a rock to a rocket.
- di4na 3y agoI am unashamedly stealing this one, it is both apt and making me smile. Kudos
- rob74 3y agoIf you're thinking of C/C++ switch statements and everyone that blindly copied them (looking at you Java, JS and PHP), you're right, but Go's switch is much more flexible (https://gobyexample.com/switch https://gobyexample.com/switch). PHP tried to correct the mistake of not giving some more thought to the switch statement at the beginning by including a new "match" expression in PHP 8 - fun times for everyone who used classes called "Match"...
- pjmlp 3y agoNowadays Java and C# switch are almost as powerful as Standard ML match.
- wholesomepotato 3y agoIt's still a rock. Play with a language with proper pattern matching, Rust, Haskell, Ocaml, Scala, and you will also see it's just a rock.
- lelanthran 3y ago> Trying to compare a switch statement to proper pattern matching is like comparing a rock to a rocket. Yeah, but when you need to bash someone over the head, the rocket suddenly isn't any use anymore. IOW, sometimes the simpler thing is better. In fact, in practice, the simpler thing is usually better... Like, rocks are usually more useful to the average person than rockets are.
- Kamq 3y agoGo's switch statement is closer to lisp's cond expression, except it doesn't evaluate to a value.
- kazinator 3y agoA switch or case statement is a restricted form of pattern matching for simple values. It fits those use cases, which are frequent. I use a language that has both pattern matching and case constructs. I use both. If you're doing complex pattern matching all over the place (especially matching the same sets of cases repeatedly in multiple locations in the code), maybe your design sucks. It's not making effective use of OOP or some other applicable organizational principle.