March 2019
Intermediate to advanced
208 pages
5h 11m
English
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 ... |
Read now
Unlock full access