Tagged Union

Noun · Development

Definitions

  1. A data type that can hold a value from a fixed set of variants, each potentially carrying different associated data, with a tag (discriminant) indicating which variant is active. Known as enum in Rust and Swift, sealed class in Kotlin, and discriminated union in TypeScript.

    In plain English: A value that can be one of several predefined types, with a label saying which one it currently is, so you never accidentally treat one kind as another.

    Example: "Model the API response as a tagged union — Success with a payload, or Error with a code and message — so the compiler forces you to handle both."

Related Terms