Sum and Product Types

In this post we will discuss sum and product types. Consider the struct X, defined as

struct X {
    A a;
    B b;
};

Here, A and B are also types, but it does not matter what their declarations are. What kind of values can an instance of type X take on? To instantiate an object of type X, we need to supply an instance of type A and an instance of type B. If the total number of different values that A can take on is denoted by \(|A|\), and the total number of different values that B can take on is denoted by \(|B|\), then the total number of values that X can take on is \(|X| = |A| \cdot |B|\).

Consider Y defined as

using Y = std::variant<A, B>;

What kind of values can an instance of type Y take on? To instantiate an object of type Y, we need to supply an instance of type A or an instance of type B. If the total number of different values that A can take on is denoted by \(|A|\), and the total number of different values that B can take on is denoted by \(|B|\), then the total number of values that Y can take on is \(|Y| = |A| + |B|\).

Because the total number of different values that X can take on is a product of its fields, we call X a product type. In contrast, we call Y a sum type, because it can only take on as many values as the sum of its constituent types.

Getting LLVM clang working on macOS 10.14.1

To get LLVM clang working on macOS 10.14.1 (Mojave), install the developer command-line tools:

xcode-select --install

assuming you first installed Xcode from the App Store.

This has been the only thing required for ages to get LLVM working on macOS. However for some reason things have changed on Mojave and we now have to install essential headers like wchar_t.h in /usr/include with an extra step. Just run this command and go through the installation wizard:

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg