From 82640a9a2b6894272d3b663cb9c365ce2d76ffee Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Fri, 28 Jun 2024 15:25:11 +0200 Subject: [PATCH] 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)