Sum and Product Types
Product Type
A product type is essentially an “and” type, a combination of types. Like a struct:
struct Foo {
bar: bool,
baz: i32
}
Foo
is a bool
AND i32
- both types are required to make Foo
.
In type theory, this is called a “product” type (for reasons I’ll explain later).
Sum Type
A sum type, on the other hand, is more like an “or” type, represented in C++ as a discriminated union:
union Foo {
bool bar;
int baz;
}
foo.bar = false;
foo.baz = 10