Steps to learning Reason OCaml
Recall the cube function:
Reason # let cube = (x) => x * x * x;
let cube: (int) => int = ;
Reason # cube;
- : (int) => int =
Reason # cube(3);
- : int = 27
When we declared the cube
function, Reason decided that it would take an int
and
return an int
. So if we try to pass it 3.5
, it throws a type error:
Reason # cube(3.5);
Error: This expression has type float but an expression was expected of type int
How would we declare a cube function that took floats? By looking at the Reason docs for
floats and ints, we can
see that multiplying floats is done with *.
:
Reason # 3.2 *. 3.2;
- : float = 10.240000000000002
Reason # let cube = (x: float) => x *. x *. x;
let cube: (float) => float = ;
Reason # cube(3.2);
- : float = 32.7680000000000078
Reason # cube(3);
Error: This expression has type int but an expression was expected of type float