What Is an Algebraic Data Type?
An algebraic data type is a composite type built by combining other types as products, which group several values together, and sums, which offer a choice among several alternatives. A value of a sum type is exactly one of its variants, each of which may carry its own data. This lets you precisely model data that can take one of several shapes.
Why it matters
Algebraic data types make invalid states unrepresentable by encoding the exact set of possibilities a value can take. They pair with pattern matching to handle each variant explicitly and safely.
Related guides
What Is a Sum Type?A sum type holds a value that is exactly one of several named alternatives, each potentially carrying differe…
What Is a Product Type?A product type groups several values together at once, like a struct or tuple, so a value carries all of its…
What Is Pattern Matching?Pattern matching inspects a value against a set of shapes, selecting a branch and binding parts of the value…