Comparison
Algebraic data typevsPattern matching
Algebraic data type
the type says it is exactly one of four things, and the compiler will not let you forget the fourth.
A type built by combining others as products — a record with several fields — or sums — one of several alternatives, each with its own data. The sum half is the one that changes how code is written: modelling a state, a result or a command as a closed set of alternatives makes the impossible combinations unrepresentable. It is the type-level version of the state pattern, and much harder to get subtly wrong.
Full entry →Pattern matching
one expression takes the value apart, names its pieces and picks a branch, all in the same line.
Branching on the shape of a value while simultaneously binding its parts. It is the natural way to consume an algebraic data type, and it is now in most mainstream languages in some form. Its practical advantage over a chain of type checks is that the compiler can see the whole set of cases, which is what makes exhaustiveness checking possible.
Full entry →