3 ms·
Serious question, is pattern matching in Elixir any different from overloading?
by speedkills 11y ago
Serious question, is pattern matching in Elixir any different from overloading?
- ericmj 11y agoPattern matching and function overloading based on types are different but can be used for the same purposes. For function overloading the decision for which implementation to use usually happens at compile time along with the type checking. Pattern matching happens at runtime but is more powerful since you can pattern match based on you guessed it: patterns, instead of just types. Another small difference is that with overloading multiple functions with different signatures are created (they just share name) but with pattern matching there is a single function with multiple "function clauses".
- speedkills 11y agoGreat explanation. Thank you.
- zzalpha 11y agoTo provide a shorter answer: polymorphism dispatches on types at compile time. Pattern matching dispatches on values at runtime.