4 ms·
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
by Analemma_ 9y ago
Pattern matching doesn't work the same way as switch/case though. Using that syntax would be misleading and confusing.
- k_ 9y agoWhy not? In Haxe (a statically typed language which compiles to js, amongst other targets) pattern matching happens in a switch: https://haxe.org/manual/lf-pattern-matching-structure.html https://haxe.org/manual/lf-pattern-matching-structure.html
- maemilius 9y agoI 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!)
- armandososa 9y agoYou'll need to insert a newline and 2 spaces before a code block to be recognized as such: switch(expr) { case 'foo': break; case { foo: bar }: break; } https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- maemilius 9y agoAh, thanks. I should have guessed it would work something like that.
- aylmao 9y agoI'd argue we should strive to move away from the switch syntax where it's not necessary because it's verbose and has curious semantics [1]. As an example, this should work: switch(expr) { case 'foo': console.log("a string") break; case { foo: bar }: console.log("foo is", bar) break; } But how would this work? switch(expr) { case 'foo': case { foo: bar }: console.log("foo is", bar) break; } Moreover, a big selling point of pattern matching is it is an expression. Keep in mind though case points to a statement list, How do we resolve to a value? With a "return"? function f() { switch(expr) { case 'foo': return 4 // This makes f return } } function g() { var x = switch(expr) { case 'foo': return 4 // But this doesn't. Is this confusing? } } The value of the last statement? function g() { var x = switch(expr) { case 'foo': 4; // This should resolve to 4 break; // but is the break necessary now? } } 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 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.
- masklinn 9y agoC's switch/case is essentially a special case with a limited set of values (literals) and no destructuring. You can extend it to less trivial patterns and blam pattern matching.
- edflsafoiewq 9y agoNo, switch is not like a special case of match. C's switch is a kind of computed goto and can build irreducible control flow graphs. match is a reducible structured control-flow construct whose power comes from destructuring. match is much, much more like an if-else chain than a switch.
- masklinn 9y ago> No, switch is not like a special case of match. Sure is. Or a degenerate one if you prefer. > match is a reducible structured control-flow construct whose power comes from destructuring. Match on sum types with no associated data and you have a switch. > match is much, much more like an if-else chain than a switch. Depends on what you're matching.
- edflsafoiewq 9y agoNo it isn't. Again, switch can build irreducible control flow, match cannot. This code switch (x) { case 0: while (i < l) { s *= i; case 1: i += 1; } } cannot be rewritten with a simple match. You'd have to reloop the CFG.