March 2019
Intermediate to advanced
208 pages
5h 11m
English
Let’s say that a small shirt costs $11.00, a medium costs $12.50, a large costs $14.00, and an extra-large costs $16.00. We can write a function to return the price of a shirt given its size:
| | let priceIf = (size: shirtSize) : float => { |
| | if (size === Small) { |
| | 11.00 |
| | } else if (size === Medium) { |
| | 12.50 |
| | } else if (size === Large) { |
| | 14.00 |
| | } else { |
| | 16.00 |
| | } |
| | }; |
| | |
| | Js.log(priceIf(mySize)); /* output: 12.5 */ |
| | Js.log(priceIf(otherSize)); /* output: 14 */ |
But it’s much more common in ReasonML to use a switch expression to pattern match the size:
| | let price = (size: shirtSize) : float |
Read now
Unlock full access