What Is a Sum Type?
A sum type, also called a tagged union or variant, is a type whose value is one of a fixed set of alternatives, with a tag identifying which one. Each alternative can carry its own kind of data. The number of possible values is the sum of the possibilities of its variants, which is where the name comes from.
Why it matters
Sum types model "one of these cases" precisely and force code to consider every case, often checked by exhaustiveness analysis. They eliminate whole classes of bugs caused by representing alternatives with loosely related flags.
Related guides
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 an Algebraic Data Type?An algebraic data type composes types as combinations of fields and alternatives, modeling data as products a…
What Is Pattern Matching?Pattern matching inspects a value against a set of shapes, selecting a branch and binding parts of the value…