|
|
|
|
|
by maemilius
2985 days ago
|
|
I think there's an argument to be made that switch statements already do something adjacent to pattern matching. At the least, it's close enough that something like: switch(expr) {
case 'foo':
break;
case { foo: bar }:
break;
}
Wouldn't strike me as all that strange. It just shifts the semantics from "are you exactly this" to "do you look like this". For non-object primitives (e.g. string or number), I don't think those two things are functionally different.Granted, it doesn't help make switch any easier to learn, but I _do_ think it makes switch more _rewarding_ to learn. Right now, switch is basically just a restrictive, potentially terser version of an if statement. This would make switch actually useful to learn. It might even allow us to add some more semantics to switch over time (e.g. constructor matching with something like 'case Number'). (EDIT: thanks to armandososa for teaching me a new thing!) |
|
With a "return"?
The value of the last statement? IMO switch sytnax is just legacy left behind by C, and not the best one to keep around, especially given the semantics of pattern matching.[1]: https://en.wikipedia.org/wiki/Duff%27s_device, not sure if this works on JavaScript (I doubt it), but the point is the switch cases essentially work like "goto"s.