February 2019
Beginner to intermediate
180 pages
4h 4m
English
Looking at our previous implementation, it would be nice if we didn't have to check whether an invoice has been discounted at runtime. Is there a way we could check whether an invoice has been discounted at compile time instead? With phantom types, we can.
Phantom types are types that have a type variable, but this type variable isn't used in its definition. To better understand, let's look again at the option type, as shown in the following code:
type option('a) = | None | Some('a);
The option type has a type variable, 'a, and the type variable is being used in its definition. As we've already learned, option is a polymorphic type because it has a type variable. On the other hand, a phantom type doesn't use the type variable ...
Read now
Unlock full access