Documentation
¶
Overview ¶
Package enum-union-sum shows how go-codex handles the three key type-modeling patterns from Go: iota enums, union types, and sum types (discriminated unions).
Each pattern maps to different codec primitives depending on whether the variant set is open or closed, and whether variants carry data:
Iota enum — closed label set, no payload → MapCodecSafe + validate.OneOf Union type — open set at runtime → codex.Any() Sum type — closed set, variants carry data → TaggedUnion / UntaggedUnion / Either2
Decision guide ¶
Pattern | Set | Variants carry data? | Codec primitive ------------------|--------|----------------------|--------------------------- Iota enum (str) | Closed | No (label only) | MapCodecSafe + OneOf Iota enum (int) | Closed | No (label only) | MapCodecSafe + RangeInt Union — open | Open | Yes (any type) | codex.Any() Sum type — tagged | Closed | Yes (different shapes)| TaggedUnion[T] Sum type — binary | Closed | Yes (two branches) | Either2
Run with: go run ./examples/enum-union-sum
Click to show internal directories.
Click to hide internal directories.