3 ms·
It is pattern matching, the semantics are an OCaml/ML family `match` expression.
by RobertKerans 6y ago
It is pattern matching, the semantics are an OCaml/ML family `match` expression.
- moonchild 6y agoAh, so it is[0]. The GP's example doesn't require pattern matching though; as I mentioned, it would work in a language lacking it. 0. https://haxe.org/manual/lf-pattern-matching-structure.html https://haxe.org/manual/lf-pattern-matching-structure.html
- yawaramin 6y agoHow so?
- moonchild 6y ago[playerA.move, playerB.move] is an array literal value, as are [Rock, Scissors] and the other controlling expressions. The switch simply performs a direct comparison between the two; there need be no destructuring for that to work.
- yawaramin 6y agoI meant 'how would it work in a language without pattern matching?' Because it seems to me that it won't. E.g. try this in JavaScript right now with a regular switch statement, it doesn't work. You need actual pattern matching.
- moonchild 6y ago'It doesn't work in one particular language that happens to lack pattern matching, therefore it cannot work without pattern matching.' In particular, pattern matching generally entails destructuring, which you do not need in order to make that particular example work.
- yawaramin 6y agoThe example is not just destructuring though. It's actually matching against a data constructor and in another branch binding a name to a value. Those are classic hallmarks of pattern matching.
- zem 6y agoit works in ruby without pattern matching, thanks to ruby's switch statement design where `switch a: case b` invokes `b.match(a)`
- RobertKerans 6y agoIt won't work in JS because arrays are compared by reference, so it would always fail to match (I think immutable data structures, at proposal stage 2, would provide one potential fix for this, otherwise need a workaround such as bitmasking the array before performing a match). It not working isn't simply that pattern matching isn't present in the language, per se.