March 2019
Intermediate to advanced
208 pages
5h 11m
English
If you have a function that consists solely of a switch expression, you can write it using the keyword fun without needing to give a parameter. The following two functions do the same thing:
| | let recip1 = (x: float): option(float) => |
| | switch (x) { |
| | | 0.0 => None |
| | | x => Some(1.0 /. x) |
| | }; |
| | |
| | let recip2 = fun |
| | | 0.0 => None |
| | | x => Some(1.0 /. x); |
This only works if the switch expression is a simple variable. You can’t convert the following switch to fun:
| | let toFloat = (s:string): option(float) => { |
| | switch (float_of_string(s)) { |
| | | result => Some(result) |
| | | exception( |
Read now
Unlock full access