Theory
A sum type provides alternatives (โeither/orโ data).
data Shape2D
= Rect Double Double -- width height
| Circ Double -- radius
| Poly [Vertex] -- โฅ3 verticesSemantics
If has values, has , then Either A B has .
Exhaustive Pattern Matching
Compiler checks every constructor is handled.
area :: Shape2D -> Double
area (Rect w h) = w * h
area (Circ r) = pi * r * r
area (Poly vs) = polygonArea vsUse sum types for error handling:
data Result a = Ok a | Err StringNOTE
Consuming code must examine both cases, preventing silent failures.