Many languages support union types, and it is high time Scala did. Union types are coming in upcoming version of Scala – Dotty. Union types (
|) are already being compared with
Option (disjoint unions).
In some ways,
Option may be expressed as union types.
// Option[String] String | null // Either[Int, String] Int | String
Similar to disjoint union types, you can pattern match over Union types. However, the differences outshine the similarities.
Disjoint union types like
- constraint the universe of types to be unique –
None. There is only one
- can be parameterized –
- Defined in the standard library. Not language syntax.
On the other hand, Union Types (
Not parameterized. Types are specific.
The types involved don’t have to be necessarily unique.
String | Int | String | Int
The above definition is valid although the universe of types is just
There is one difference that stands out to me, in fact of disjoint union types.
Option are monads and so they give the niceties of
flatMap and all those of a container. Can’t do that with Union types.
Update: Per phazer99‘s reddit comment, union types could be augmented with extension methods to get the
flatMap. That’s interesting although I feel it does not make it truly monadic. For instance, what is the unit value? Will the monad laws hold good? Happy to stand corrected.
However, Union types give you edge over one thing. In fact, the thing I don’t like about
Option; or JVM in general. Disjoint union types are allocated on the heap; every instance. Union types are compile time construct and do not require extra allocation.
As you can see, grass is greener on either side. Both disjoint union and Union types have their place and are here to stay. You gotta choose the right one for the job;
Either disjoint types
| Union Types!