jiahao.blog

20 Sept 2026

Finally Understanding Union and Product Types

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

Product types

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:

Union types

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.

Sum types

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 , since the available values are now a summation of the two aliased values (this isn’t the same as a product type since the total outcomes are not a combination of these fields, it’s still an EITHER OR).

Enjoyed reading?

Consider subscribing to my RSS feed or reaching out to me through email!

You might enjoy...

20 Jun 2026

Hosting a Minecraft Server on a DigitalOcean Droplet

09 Feb 2026

Creating a CHIP-8 emulator

08 Oct 2025

Optimizing Github Actions for Git-Mastery