Is reason not support "function" keyword?


#1

I try to do this:

let call = function {
| Some x => x * 2
| None => 0
};

but it looks not be support. it is work in ml file.

is that true?


#2

@andares I’m new to this too but I’ll take a crack at answering. I believe you are correct, in ReasonML you have to use arrow function syntax. let fun = () => ...

Also () is shorthand for unit type, because all functions need an input type. In your example above (x) => … looks like it function has type: option(int) -> int

I believe it would be confusing to have two different ways to write a function (like there is currently in JS), so in Reason there is only one way.


#3

In Reason we use fun instead of function.

let call = fun
| Some(x) => x * 2
| None => 0

There is an example of this in the Comparison to OCaml section of the docs. Might be good to give that whole section a read through if you’re coming from OCaml.


#4

I wasn’t aware there was a fun keyword. So I think my answer is wrong.
I found this issue: https://github.com/facebook/reason/issues/1405
Which left me thoroughly confused, lol!