|
|
|
|
|
by mc3
2409 days ago
|
|
What is the benefit of pattern matching in a language where any value can be of any type at runtime? You can just pattern match by saying if (obj.type === 'foo') {}; if (obj.type === 'bar') {}; etc in JS, as long as you have set up the convention that you set the 'type' field, and practically this is just as good. The real benefit of pattern matching is the compiler checking at least that you have valid cases, and ideally that you haven't missed any cases. |
|
The power of pattern matching grows clearer as more complex data and rules are introduced. For example, it works just as well with nested lists:
While having a type system is helpful in all of the ways a type system is generally helpful, there’s nothing about this code which wouldn’t also be handy in a dynamic language like JavaScript. For further evidence of this see Erlang and Elixir, dynamic languages which make heavy use of pattern matching.(Code typed on my iPhone so forgive the dopey examples)