ELSEIF
Your brief EB
207 stories from 202 feeds 1253 clusters Refreshed 29 minutes ago next pull 07:01

TECH Signal 337

Union types deduplicate identical members while sum types preserve distinct tags

Illustration only Photo by Spencer Watson on Unsplash

Union types treat members as sets of values, causing identical types to collapse into one, whereas sum types use tags to keep structurally identical but semantically distinct values separate.

WHY IT MATTERS

Choosing between these concepts determines whether your type system automatically simplifies redundant data or forces explicit disambiguation. This distinction is critical when modeling data where representation overlaps but meaning differs, such as different calendar systems stored in the same integer width.

Written by elseif from the cluster below · every claim links back to a source

The three things worth knowing

01

Union types are defined by the set union of their members, meaning duplicate types like bool OR bool simplify to just bool.

02

Sum types, or tagged unions, wrap members in distinct structs to prevent conflation of values that share the same underlying representation.

03

The choice impacts type inference and set operations, as unions allow mathematical simplification of return types while sum types maintain strict separation.

THE READ

What the cluster adds up to.

ORIGINAL ANALYSIS

The core distinction between union and sum types lies in how they handle the set of possible values. A union type is mathematically defined as the union of its member sets, which means if two members have identical value spaces, they collapse into a single type. For example, a union of two bool types simplifies to just bool, because the set of possible values remains {false, true}. This behavior makes unions suitable for expressing uncertainty or combining overlapping type spaces, where the specific origin of a value is irrelevant.

Sum types, also known as tagged unions, solve the problem of conflation by wrapping each member in a distinct tag. If you need to distinguish between a Gregorian year and a Hijri year, both stored as u16, a simple union would collapse them into a single u16, losing the semantic distinction. By creating separate structs for each variant, the type system forces the program to explicitly handle the tag, ensuring that data is interpreted correctly despite identical underlying representations. This adds a layer of safety for domains where structural similarity does not imply semantic equivalence.

The practical implication for engineers is that union types facilitate type inference and simplification in type systems that support set operations. If a function can return either u16 or i16, and another returns u16 or u32, the combined return type of calling either function simplifies to the union of all three types. This automatic deduplication reduces the cognitive load of tracking type possibilities. However, this same mechanism is a liability when distinct concepts share a representation, requiring the developer to switch to sum types to maintain data integrity.

Adopting sum types over unions incurs a cost in verbosity, as each variant must be explicitly defined and tagged. This is a necessary trade-off for correctness in complex data models, but it can feel redundant for simple cases where the distinction is trivial. The decision to use one over the other should be driven by whether the data's meaning depends on its origin or only on its value. If the origin matters, sum types are mandatory; if it does not, unions provide a more concise and mathematically consistent model.

Written by elseif from the cluster below · checked for specifics the sources never contained

THE CLUSTER

Same story, 1 feed.

ORDERED BY FIRST SEEN
viralinstruction.com via Lobsters Union vs sum types Open ↗