enum-union-sum

command
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 7 Imported by: 0

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL