Reason: Wrong syntax for functions in docs?

Created on 24 Apr 2017  路  6Comments  路  Source: reasonml/reason

Hope this is not me being silly, but it looks like a couple of funs on index.html need to be changed to lets. Under the _Function_ section:

fun greet name => "Hello " ^ name;

/* - : 'a => string => string = <fun> (definitely not what you want) */

fun greetMore name => {
  let part1 = "Hello";
  part1 ^ " " ^ name
};

/* - : 'a => string => string = <fun> (again not what you want)

Most helpful comment

@hessammehr thanks for the feedback. I've modified the docs to stay concise while addressing your point. Hope the new one works well!

All 6 comments

As the docs explain in the next section, functions in reason are first class, meaning you can pass them around like any other value. I think the intention here was to show how to create a function. The following section shows how to assign one of these functions to a name using a let binding.
ie
fun x y => x + y is a value
let add = fun x y => x + y assigns this value to the name increment

A little further down the page, the docs give an example of a commonly-used piece of sugar for defining functions and binding them to a name.
let add x y => x + y;
Hope that helps a little!

Thanks for the examples, @Schmavery. The first example of a function in the docs, fun greet name => "Hello " ^ name, is not a function that greets the user (as its name might suggest and the author probably intended). Rather, it takes a greet of type 'a and a string. This looks like an error to me.

Just to be clear, the correct example would be let greet name => "Hello " ^ name or let greet = fun name => "Hello " ^ name, as you have pointed out.

Yeah I screwed up, sorry. cc @bsansouci
Thanks for pointing this out!

No worries, @chenglou, glad I could help. At any rate, I think this is a bit of an odd introduction to functions since no invocations are shown here, which would confuse the hell out of someone without a functional background. It is kind of awkward to add examples, too:

(fun name => "Hello " ^ name) "world!"; /* "Hello world!" */

Perhaps this would work better (then you explain the syntactic sugar ...):

let greet name => "Hello " ^ name;
greet "world!"; /* "Hello world!" */

Just my two cents.

@hessammehr thanks for the feedback. I've modified the docs to stay concise while addressing your point. Hope the new one works well!

@chenglou thanks, looks great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ostera picture ostera  路  3Comments

chenglou picture chenglou  路  3Comments

aaronshaf picture aaronshaf  路  3Comments

TheSpyder picture TheSpyder  路  3Comments

modlfo picture modlfo  路  4Comments