44 ms·
Yep, these are called "discriminated unions" in F#. I think most functional programming languages have something similar. The underlying concept is "algebraic
by brianberns 8y ago
Yep, these are called "discriminated unions" in F#. I think most functional programming languages have something similar.
The underlying concept is "algebraic data types": You can compose types as products (tuples or records) and sums (discriminated unions) of simpler types. From there, you can actually use algebra on types, which is pretty mind-blowing. For example, we can define a generic sum type called Either:
Either a b = Left a | Right b
You can think of Either as the sum of types "a" and "b": a + b. Tuples are product types, so you can use the distributive property of multiplication over addition to establish that:
(a, Either b c) = Either (a, b) (a, c)
Or algebraically:
a * (b + c) = (a * b) + (a * c)
This is one of those areas where category theory is actually useful for programmers.