Just a quick blog about how to represent discriminated unions, also called sum types.
A union type allows you to store one of N distinct types in the same variable. What makes it discriminated is that, if the types overlap, you should still be able to tell the cases apart.
So for example maybe we want to define a variable context
which might store either a FooContext or a BarContext or a BazContext, all of which are nullable types (for other reasons in our code, let's say). When we store a null FooContext in context
, we want context
to still be able to tell us that it's holding a FooContext, just the null version of that. That makes it a discriminated union.
These are called "sum types" because this lack of overlap means that if there were two valid values for FooContext (the null one and some global one, say), three for BarContext, and four for BazContext, the number of valid values for context
would be 2+3+4. It could have been less than this if they were allowed to "overlap