Re:JavaScript Pattern Matching Proposal
by masklinn on Wednesday April 25, 2018 @12:57PM
(#16923518)
C'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.
[ Parent
| on HN ]
Re:JavaScript Pattern Matching Proposal
by edflsafoiewq on Wednesday April 25, 2018 @04:17PM
(#16925619)
No, 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.
> 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.
Re:JavaScript Pattern Matching Proposal
by edflsafoiewq on Thursday April 26, 2018 @10:49AM
(#16932336)
No 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.