January 2018
Beginner to intermediate
312 pages
7h 22m
English
An invariant is a condition that stays true no matter what else happens. For example, at the beginning of the chapter, we said that a UnitQuantity must always be between 1 and 1000. That’s an example of an invariant.
We also said that there must always be at least one order line in an order. Unlike the UnitQuantity case, this is an example of an invariant that can be captured directly in the type system. To make sure that a list isn’t empty, we just need to define a NonEmptyList type. It’s not built into F#, but it’s easy to define yourself:
| | type NonEmptyList<'a> = { |
| | First: 'a |
| | Rest: 'a list |
| | } |
The definition itself requires that there must always be at least one element, so a NonEmptyList ...
Read now
Unlock full access