Tagged unions
A tagged union defines a closed set of alternatives. Each alternative has a name and may carry a value:
public union SearchResult Found(value: String) MissingendA SearchResult is either Found with a String payload named value or Missing with no payload. The cases are constructed through the union name:
local success: SearchResult = SearchResult.Found("Ada")local failure: SearchResult = SearchResult.MissingThe tag records which case is present. Pop checks the payload at construction, so SearchResult.Found(42) is rejected.
Why use a union?
Section titled “Why use a union?”A union makes alternatives explicit in the type system. Compare a search function returning only String with one returning SearchResult. The second signature tells its caller that absence is a normal outcome that must be handled.
Unions also model states without relying on magic numbers or loosely related Boolean flags:
public union Connection Disconnected Connecting Connected(String)endOnly the listed states can exist, and only Connected carries the peer name.
Union equality is structural when equality exists for every payload type. Values with different case tags are unequal. Values with the same tag compare their payloads.
Use match to discover the active case and safely access its payload.
