WEP: Union Types
Context
WEP: Variant Payload Design proposed anonymous union types beside named variants, and left them unbuilt. This WEP takes them over, with what the web bindings have since shown about them.
WebIDL writes unions everywhere, and
WEP: The Web Interface for Wado lowers each one to a
variant it generates. That costs a web program in three ways:
- Every call spells the case:
div.append([NodeOrString::AsString("text")]). - The generated names grow with the constituents, as
F64OrDomPointInitOrF64OrDomPointInitListdoes, and two unions that differ can end up with one name. - A union's type changes as the slice grows.
set_attributetakes aStringwhileTrustedHTMLis outside the slice, and aTrustedHtmlOrStringonce it joins, so every caller breaks.
Wado means to be a language for web front ends, so a union should be something a program writes as the platform does.
What users of other languages complain about:
- TypeScript: unions themselves are well liked. The complaints are about narrowing that is hard to follow, and about holes in structural typing's soundness.
- OCaml's polymorphic variants: powerful, and known for error messages that are hard to read.
- Rust: there is no anonymous sum, so a program writes a small
enumand itsFromimpls by hand. That is where Wado's generated web variants are today.
Decision
These come from WEP: Variant Payload Design.
A | Bis an anonymous sum type with set semantics. Internally it carries a tag, as a variant does.- Any type may be a constituent, a primitive or a variant's case included:
i32 | String,Shape::Circle | Shape::Rectangle. - A union flattens and drops duplicates:
(A | B) | (B | A)isA | B. A union of one type,A | A, isUnion<A>, notA, so every union is taken apart the same way. - Subset binding takes a union apart with a type pattern, into one
constituent or a smaller union:
if let ab: A | B = abcd, andmatch x { ab: A | B => …, c: C => … }. - A
matchover a union is checked for exhaustiveness.
Open questions
- Representation: whether a union is represented as a
variant, and so crosses the Component Model boundary as one. - WebIDL: whether a WebIDL union lowers to a union type in place of a generated variant.
- Injection: whether a value of a constituent's type is accepted where the
union is expected, so that
set_attribute("id", "x")compiles. Also whether anA | Bis accepted where anA | B | Cis. - Overlap: constituents related by
extends, asNode | Elementare, or a type and a newtype over it. Which case a value takes and which pattern matches it are both ambiguous. WebIDL requires a union's constituents to be distinguishable. - Literals: which case an integer literal takes in
f64 | i64. - Generics: what
T | Uis whereTandUturn out to be one type. - Order: the earlier WEP orders constituents by first occurrence. At the
Component Model boundary,
A | BandB | Aare one type only if they lower to one case order, and the cases need names there. null: whetherT | nullexists besideOption<T>, which WebIDL's nullable types lower to today.- Inference: whether
if c { 1 } else { "a" }infers a union, or a union needs an expected type.
Roadmap
- [ ] Union type syntax (
A | B) - [ ] Union type normalization (flattening, deduplication to
Union<A>) - [ ] Discriminant generation for union types
- [ ] Subset binding in
if let(if let x: A | B = abc) - [ ] Subset binding in
match - [ ] Exhaustiveness checking for union types
