4 ms·
Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing. 1. A union type is like int | double. It means the value is one of
by millstone 6y ago
Simple way to think about it is plus. Sum plus sum increases, union plus union does nothing.
1. A union type is like int | double. It means the value is one of a set of possible types. And it's a true set: `int | int | double` is indistinguishable from `int | double`.
2. A sum type is a new type built from a list of other types, which assigns a 'tag' to each possible list element, like a Rust enum. The tags are not themselves types: they are like a struct field name. It's quite possible to have a sum type with N tags, each wrapping `int`.
So with this understanding Rust has sum types but not union types, while TypeScript has union types but not sum types.
- amitport 6y agoThanks! This clarifies the situation
- paavohtl 6y agoIt is possible (and popular) to have sum types in TypeScript by manually adding a tag, though. type Maybe<T> = | { kind: "Some", value: T } | { kind: "None" }
- carlmr 6y agoThis is also why sum types are often referred to as tagged unions. They are a special case of a union, in Rust's case with support from the type system.
- a_humean 6y agoYep, Typescript's control flow analysis, type literals, and union types allow us have tagged unions/sum types. Probably its best feature.
- tybit 6y agoTypeScript does have tagged unions in addition to untagged ones https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#tagged-union-types https://www.typescriptlang.org/docs/handbook/release-notes/t...
- cdirkx 6y agoRust also has 'union' types [1] that work similar to the union types in C, C++. Recently there has also been a lot of discussion [2] about possibly adding anonymous enums to the language, and whether these should have sum or union semantics. [1]https://doc.rust-lang.org/reference/items/unions.html https://doc.rust-lang.org/reference/items/unions.html [2]https://internals.rust-lang.org/t/ideas-around-anonymous-enum-types/12627/207 https://internals.rust-lang.org/t/ideas-around-anonymous-enu...
- karmakaze 6y agoI've always considered both the union and tagged union to be Sum types[0]. > Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e., tagged or disjoint unions, coproduct types or variant types). Edit: e.g. X | Y isn't so different from 'a' A | 'b' B [0] https://en.wikipedia.org/wiki/Algebraic_data_type https://en.wikipedia.org/wiki/Algebraic_data_type
- nybble41 6y ago> X | Y isn't so different from 'a' A | 'b' B Except for the fact that the union of a type with itself (X | X) doesn't add any extra values, while a tagged union can have distinct tags with the same data type ('a' A | 'b' A) and actually sums the possible values for each tag. They are equivalent only so long as the unions are disjoint with no overlap between the members.
- karmakaze 6y agoI think we're saying the same thing. I was saying it's like thinking of 'a' A as an X and 'b' B as a Y.
- throwaway894345 6y agoWRT to 2, one useful property is that you can have two or more labels for the anonymous type `()`, such as `enum MyBool { True, False }` or `enum JSON { List(Array<JSON>), Object(List<(String, JSON)>), Number(f64), String(String), True, False, Null }`.