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. Choose Edit & run in-page below (or open it in the playground), press Run
to describe one value of each variant, then try deleting a branch.
context demo
---A sum type: a `Status` is exactly one of these shapes, and the variants thatcarry 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 theprogram, 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 }}
---Run calls `service playground`, which describes one value of each variant.---service playground { on call() -> Effect[List[String]] by Caller { let all: List[Status] = [Pending, Shipped("1Z999"), Cancelled("out of stock")] all.map((s) => describe(s)) }}