In this lesson we walk through how to declare functions in ReasonML. Declaring and using functions is straightforward. Still there are a handful great features not available in a lot of other languages like labeled arguments or auto currying. Both are explained in this lesson.
What do you mean with avoid a lot of tedious switch expressions (06:53)? The example uses a function with a switch
in it, can you please provide another example?
The example uses ~middleName=?someName
. Without that you would need to switch on someName
. Does that help?
I believe the behaviour described in the video has become outdated.
In the latest version of rtop (2.2.0), I get the following output:
Reason # let name = (~firstName, ~middleName="Francis", ~lastName) => {
firstName ++ " " ++ middleName ++ " " ++ lastName;
};
let name:
(~firstName: string, ~middleName: string=?, ~lastName: string) => string =
<fun>;
Reason # name(~firstName="Jane", ~middleName="Kim", ~lastName="Doe");
- : string = "Jane Kim Doe"
Reason # name(~firstName="Jane", ~lastName="Doe");
- : (~middleName: string=?) => string = <fun>
Reason # name(~firstName="Jane", ~lastName="Doe")();
Error: The function applied to this argument has type
(~middleName: string=?) => string
This argument cannot be applied without label
Reason # name(~firstName="Jane", ~lastName="Doe", ());
Error: The function applied to this argument has type
(~middleName: string=?) => string
This argument cannot be applied without label
sorry, please ignore the above comment, I didn't notice the positional parameter ()
at the end!
```name(~firstName="Jane", ~middleName="Kim", ~lastName="Doe", ());`