March 2019
Intermediate to advanced
208 pages
5h 11m
English
A data type alias is the simplest of all custom data types—it gives a new name to an existing data type. For example, if you want types to represent test scores and percents, you could do this:
| | type scoreType = int; |
| | type percentType = float; |
| | |
| | let calcPercent = (score: scoreType, max: scoreType) : percentType => |
| | float_of_int(score) /. float_of_int(max) *. 100.0; |
The only advantage of using a data type alias is to increase readability, though you might also achieve this by using labeled parameters. Aliases don’t give you any type safety. You can still write meaningless code like this:
| | type scoreType = int; |
| | type percentType = float ... |
Read now
Unlock full access