3 ms·
> I may be wrong, but I don't think this is what people mean by 'sum types'. It is. > I was expecting union types (e.g., https://www.typescriptlang.org/docs/h
by masklinn 6y ago
> I may be wrong, but I don't think this is what people mean by 'sum types'.
It is.
> I was expecting union types (e.g., https://www.typescriptlang.org/docs/handbook/unions-and-inte.. https://www.typescriptlang.org/docs/handbook/unions-and-inte.... or https://dotty.epfl.ch/docs/reference/new-types/union-types.h.. https://dotty.epfl.ch/docs/reference/new-types/union-types.h.... )
Union types are basically an anonymous form of sum types. In the same way tuples and structs / records are both forms of product types.
Statically typed languages which support sum types generally only support the named version (because it tends to be more useful and powerful).
> I guess enums are a type of sum type, but seems this is more specifically about enums
Depends on the language, C's enums are not types at all, Java's or C++'s are product types. Technically enums (or more generally tagged unions, which is what Rust's enums are) is a superset of sum types as you can have multiple variants of the same type, but that's not leveraged here.
- curryhoward 6y ago> Union types are basically an anonymous form of sum types. This is false on multiple levels. First, being a sum type has nothing to do with the type being named vs. anonymous. What makes a sum type a sum type is that it's a categorical coproduct, whether you give it a name or not. Second, sum types are synonymous with _disjoint_ (i.e., tagged) unions, not unions. Consider the union of boolean with itself. The result would be equivalent to boolean, because union is an idempotent operation. Disjoint union, or sum, would give you a type with 4 values instead of 2. > Technically enums (or more generally tagged unions, which is what Rust's enums are) is a superset of sum types as you can have multiple variants of the same type That just how sum types work (have you ever wondered why they are called "sum" in the first place?). You're thinking of union types.