March 2019
Intermediate to advanced
208 pages
5h 11m
English
Here’s where ReasonML’s type system begins to show some of its power. Let’s say we want a data type to represent shirt sizes: Small, Medium, Large, and XLarge (extra-large). We could use an alias for the string type, but it wouldn’t keep us from doing things like this:
| | type shirtSize = string; |
| | |
| | let mySize = "Medium"; |
| | let otherSize = "Large"; |
| | let wrongSize = "M"; |
ReasonML lets us create a data type that allows only valid values with a data type constructor, which, as its name implies, tells us how to construct a value of that particular data type. This is called a variant data type, as we’re specifying the various values the data type can have:
Read now
Unlock full access