From c9e4a4b8f16aed80161bcca2a7b2b86fe31ed0a7 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 14 Jun 2024 23:44:36 +0200 Subject: [PATCH 1/5] Syntax Lookup: Add labeled args --- .../syntax/language_labeled_argument.mdx | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 misc_docs/syntax/language_labeled_argument.mdx diff --git a/misc_docs/syntax/language_labeled_argument.mdx b/misc_docs/syntax/language_labeled_argument.mdx new file mode 100644 index 000000000..7d5eceb38 --- /dev/null +++ b/misc_docs/syntax/language_labeled_argument.mdx @@ -0,0 +1,36 @@ +--- +id: "labeled-argument" +keywords: ["labeled", "argument"] +name: "~arg" +summary: "This is a `labeled argument`." +category: "languageconstructs" +--- + +When declaring a function, arguments can be prefixed with `~` which means that they can and need to be called by their name, not the argument position. This is especially useful to differentiate them more easily if they are of the same type. + +### Example + + + +```res +let calculateDistance = (~x1, ~y1, ~x2, ~y2) => { + Math.sqrt((x1 -. x2) ** 2. +. (y1 -. y2) ** 2.) +} + +calculateDistance(~x1=6., ~y1=8., ~x2=3., ~y2=4.) +``` + +```js +function calculateDistance(x1, y1, x2, y2) { + return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); +} + +calculateDistance(6, 8, 3, 4); +``` + + + +### References + +* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments) +* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks) From 210a9086a99a6a9d6eaf69bdcaee81953f269ebb Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 14 Jun 2024 23:44:44 +0200 Subject: [PATCH 2/5] Syntax Lookup: Add optional args --- .../language_optional_labeled_argument.mdx | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 misc_docs/syntax/language_optional_labeled_argument.mdx diff --git a/misc_docs/syntax/language_optional_labeled_argument.mdx b/misc_docs/syntax/language_optional_labeled_argument.mdx new file mode 100644 index 000000000..02d53baea --- /dev/null +++ b/misc_docs/syntax/language_optional_labeled_argument.mdx @@ -0,0 +1,48 @@ +--- +id: "optional-labeled-argument" +keywords: ["optional", "labeled", "argument"] +name: "~arg=?" +summary: "This is an `optional labeled argument`." +category: "languageconstructs" +--- + +Labeled arguments, i.e. arguments that are prefixed with `~`, can be suffixed with `=?` to denote that they are optional. Thus, they can be +omitted when calling the function. + +### Example + + + +```res +let print = (text, ~logLevel=?) => { + switch logLevel { + | Some(#error) => Console.error(text) + | _ => Console.log(text) + } +} + +print("An info") +print("An error", ~logLevel=#error) +``` + +```js +function print(text, logLevel) { + if (logLevel === "error") { + console.error(text); + } else { + console.log(text); + } +} + +print("An info", undefined); + +print("An error", "error"); +``` + + + +### References + +* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments) +* [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments) +* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks) From a974e65fb01a3200e903b39b04e8e4b2966d583d Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 14 Jun 2024 23:45:25 +0200 Subject: [PATCH 3/5] Syntax Lookup: Make send.pipe deprecated --- misc_docs/syntax/decorator_send_pipe.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/misc_docs/syntax/decorator_send_pipe.mdx b/misc_docs/syntax/decorator_send_pipe.mdx index c3f4b8125..f42035863 100644 --- a/misc_docs/syntax/decorator_send_pipe.mdx +++ b/misc_docs/syntax/decorator_send_pipe.mdx @@ -4,6 +4,7 @@ keywords: ["send", "pipe", "decorator"] name: "@bs.send.pipe" summary: "This is the `@bs.send.pipe` decorator." category: "decorators" +status: "deprecated" --- > Removed since compiler version 10.0. Use the [@send](https://rescript-lang.org/docs/manual/latest/bind-to-js-function) decorator instead. From 82640a9a2b6894272d3b663cb9c365ce2d76ffee Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 28 Jun 2024 15:25:11 +0200 Subject: [PATCH 4/5] Syntax Lookup: Default labeled args --- .../language_optional_labeled_argument.mdx | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_optional_labeled_argument.mdx b/misc_docs/syntax/language_optional_labeled_argument.mdx index 02d53baea..e4e7e7540 100644 --- a/misc_docs/syntax/language_optional_labeled_argument.mdx +++ b/misc_docs/syntax/language_optional_labeled_argument.mdx @@ -13,7 +13,7 @@ omitted when calling the function. -```res +```res example let print = (text, ~logLevel=?) => { switch logLevel { | Some(#error) => Console.error(text) @@ -41,8 +41,45 @@ print("An error", "error"); +Optional labeled arguments can also hold a default value. + + + +```res example +let print = (text, ~logLevel=#info) => { + switch logLevel { + | #error => Console.error(text) + | #warn => Console.warn(text) + | #info => Console.log(text) + } +} + +print("An info") +print("A warning", ~logLevel=#warn) +``` + +```js +function print(text, logLevelOpt) { + var logLevel = logLevelOpt !== undefined ? logLevelOpt : "info"; + if (logLevel === "warn") { + console.warn(text); + } else if (logLevel === "error") { + console.error(text); + } else { + console.log(text); + } +} + +print("An info", undefined); + +print("A warning", "warn"); +``` + + + ### References * [Labeled Arguments](/docs/manual/latest/function#labeled-arguments) * [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments) +* [Labeled Argument with Default Value](/docs/manual/latest/function#optional-with-default-value) * [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks) From 22a4b9ade029e9878d89f244d65954fcee8b4e91 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 28 Jun 2024 15:25:50 +0200 Subject: [PATCH 5/5] Syntax Lookup: Mention partial application and order of labeled args --- .../syntax/language_labeled_argument.mdx | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/misc_docs/syntax/language_labeled_argument.mdx b/misc_docs/syntax/language_labeled_argument.mdx index 7d5eceb38..c775614f4 100644 --- a/misc_docs/syntax/language_labeled_argument.mdx +++ b/misc_docs/syntax/language_labeled_argument.mdx @@ -12,7 +12,7 @@ When declaring a function, arguments can be prefixed with `~` which means that t -```res +```res prelude let calculateDistance = (~x1, ~y1, ~x2, ~y2) => { Math.sqrt((x1 -. x2) ** 2. +. (y1 -. y2) ** 2.) } @@ -30,6 +30,39 @@ calculateDistance(6, 8, 3, 4); +Labeled arguments can be provided in any order: + + + +```res example +calculateDistance(~x1=6., ~x2=3., ~y1=8., ~y2=4.) +``` + +```js +calculateDistance(6, 8, 3, 4); +``` + + + +This also works together with partial application: + + + +```res example +let calcY = calculateDistance(~x1=6., ~x2=3., ...) +calcY(~y1=8., ~y2=4.) +``` + +```js +function calcY(none, extra) { + return calculateDistance(6, none, 3, extra); +} + +calcY(8, 4); +``` + + + ### References * [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)