Creating Variant Data Types with Parameters
Shirt sizes don’t end with extra-large. There are double, triple, and even quadruple extra-large, usually abbreviated as XXL, XXXL, and XXXXL. Parameterized types let us specify a parameter for the constructor. In our case, we want the parameter to tell us how many Xs are on the shirt size. Here’s a parameterized version of the shirt size constructor (it has the same name, but it’s in a separate file):
| type shirtSize = |
| | Small |
| | Medium |
| | Large |
| | XLarge(int); |
The last line says that to construct an XLarge variant, you need to provide an integer, which we’ll use to tell how many “extras” we want:
| let mySize ... |
Get Web Development with ReasonML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.