From 1d56a3a9dc6293d34a2b32e5ef59531e0cb7e632 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 3 Dec 2018 17:05:03 -0600 Subject: [PATCH] Add Using Optional Labeled Function Arguments as a reason til --- README.md | 3 +- ...ing-optional-labeled-function-arguments.md | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 reason/using-optional-labeled-function-arguments.md diff --git a/README.md b/README.md index 8090de3..bdeb113 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_727 TILs and counting..._ +_728 TILs and counting..._ --- @@ -542,6 +542,7 @@ _727 TILs and counting..._ - [String Interpolation With Quoted Strings](reason/string-interpolation-with-quoted-strings.md) - [Trying Out ReasonML In CodeSandbox](reason/trying-out-reasonml-in-codesandbox.md) - [Two Ways To Find An Item In A List](reason/two-ways-to-find-an-item-in-a-list.md) +- [Using Optional Labeled Function Arguments](reason/using-optional-labeled-function-arguments.md) - [Wrapping A Component For Use In JavaScript](reason/wrapping-a-component-for-use-in-javascript.md) ### Ruby diff --git a/reason/using-optional-labeled-function-arguments.md b/reason/using-optional-labeled-function-arguments.md new file mode 100644 index 0000000..ef61dc2 --- /dev/null +++ b/reason/using-optional-labeled-function-arguments.md @@ -0,0 +1,32 @@ +# Using Optional Labeled Function Arguments + +If you are constructing a function that takes some arguments, but one of +those arguments has a reasonable default value, then you can use an optional +labeled argument. Labeled arguments are those arguments prefixed with a `~`. +If you give the argument a default value, then it becomes optional. + +```reason +let thing = (~a=1, b: int, c: int) => { + a + b + c; +}; +``` + +In this case `~a` is a labeled argument. It is also optional and will +default to `1` if not specified. The other two arguments, `b` and `c`, are +positional arguments and thus required in order for the function to +evaluate. + +Here are two ways of using this function either by specifying `~a` or +excluding it so that it defaults to `1`. + +```reason +thing(~a=2, 1, 1) +|> string_of_int +|> print_endline /* 4 */ + +thing(1, 1) +|> string_of_int +|> print_endline /* 3 */ +``` + +See more details [here](https://reasonml.github.io/docs/en/function).