Skip to content

Commit

Permalink
Syntax Lookup: Default labeled args
Browse files Browse the repository at this point in the history
  • Loading branch information
fhammerschmidt committed Jun 28, 2024
1 parent a974e65 commit 82640a9
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion misc_docs/syntax/language_optional_labeled_argument.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ omitted when calling the function.

<CodeTab labels={["ReScript", "JS Output"]}>

```res
```res example
let print = (text, ~logLevel=?) => {
switch logLevel {
| Some(#error) => Console.error(text)
Expand Down Expand Up @@ -41,8 +41,45 @@ print("An error", "error");

</CodeTab>

Optional labeled arguments can also hold a default value.

<CodeTab labels={["ReScript", "JS Output"]}>

```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");
```

</CodeTab>

### 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)

0 comments on commit 82640a9

Please sign in to comment.