Skip to content

Sum types and match

A sum type says a value is exactly one of several alternatives, and the variants that carry data carry it by name — Shipped has a tracking, Cancelled has a reason. There is no need for a tag field and a nullable payload; the shape and its data travel together.

What makes this more than a tidy enum is exhaustiveness. match must account for every variant: leave one out and the program is rejected, so adding a new Status forces you to handle it everywhere it is matched. Open it in the playground and try deleting a branch.

sum-type-and-match.bynk
commons demo
---
A sum type: a `Status` is exactly one of these shapes, and the variants that
carry data carry it by name.
---
type Status =
| Pending
| Shipped(tracking: String)
| Cancelled(reason: String)
---
`match` is exhaustive — leave a variant out and the compiler rejects the
program, so a new status can never be silently unhandled.
---
fn describe(s: Status) -> String {
match s {
Pending => "awaiting shipment"
Shipped(tracking: t) => t
Cancelled(reason: r) => r
}
}