Working with Types
To see an example of ReasonML’s attention to detail about types, let’s modify the preceding price calculation program to handle prices that aren’t integers, such as $14.50. If we change the code to read:
| let qty = 7; |
| let price = 14.50; |
| let total = qty * price; |
| Js.log2("The total price is $", total); |
We get this compile error:
| We've found a bug for you! |
| /path/to/code/first-project/src/FloatPrice.re 3:19-23 |
| |
| 1 │ let qty = 7; |
| 2 │ let price = 14.50; |
| 3 │ let total = qty * price; |
| 4 │ Js.log2("The total price is $", total); |
| |
| This has type: |
| float |
| But somewhere wanted: |
| int |
| |
| You can convert a float to a int with int_of_float. |
| If this is a literal, you want a number without a trailing ... |
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.