20 Sept 2026
2 min read
I was reading this viralinstruction blog post about Union vs Sum types and these types finally clicked in my head.
The key focus here is the available and possible values when dealing with these types. For instance, a type of bool has two possible values: true and false.
For now, we will indicate the available values of a type as
struct Foo {
is_bar: bool
baz: int16
}
Structs are an example of a product type because the possible values are the product of all possible values of each field. So values Foo{is_bar: true, baz: 1} and Foo{is_bar: true, baz: 2} and Foo{is_bar: false, baz: 1} are all in the space of possible values for this struct.
Notationally, this product type’s available value space would be:
type Foo = bool | int16
The available values are the values of bool OR the values of int16, not both.
Notationally:
The benefit of this is that we effectively de-duplicate the available values. For instance, type Foo = bool | bool would have the available values of a single bool.
type Foo = bool
type Bar = bool
type Baz = Foo | Bar
Sum types are tagged or discriminated unions. Essentially, they are formed by wrapping a type in an alias. Doing so allows the available values to not be de-duplicated.
In the example above, although the underlying type is type Baz = bool | bool, the available values isn’t just a single bool, it is
Enjoyed reading?
Consider subscribing to my RSS feed or reaching out to me through email!