Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Ah, 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



How so?


[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.


I 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.


It 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.


'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.


The 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.


it works in ruby without pattern matching, thanks to ruby's switch statement design where `switch a: case b` invokes `b.match(a)`




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: