Skip to content

Commit 210a908

Browse files
Syntax Lookup: Add optional args
1 parent c9e4a4b commit 210a908

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
id: "optional-labeled-argument"
3+
keywords: ["optional", "labeled", "argument"]
4+
name: "~arg=?"
5+
summary: "This is an `optional labeled argument`."
6+
category: "languageconstructs"
7+
---
8+
9+
Labeled arguments, i.e. arguments that are prefixed with `~`, can be suffixed with `=?` to denote that they are optional. Thus, they can be
10+
omitted when calling the function.
11+
12+
### Example
13+
14+
<CodeTab labels={["ReScript", "JS Output"]}>
15+
16+
```res
17+
let print = (text, ~logLevel=?) => {
18+
switch logLevel {
19+
| Some(#error) => Console.error(text)
20+
| _ => Console.log(text)
21+
}
22+
}
23+
24+
print("An info")
25+
print("An error", ~logLevel=#error)
26+
```
27+
28+
```js
29+
function print(text, logLevel) {
30+
if (logLevel === "error") {
31+
console.error(text);
32+
} else {
33+
console.log(text);
34+
}
35+
}
36+
37+
print("An info", undefined);
38+
39+
print("An error", "error");
40+
```
41+
42+
</CodeTab>
43+
44+
### References
45+
46+
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
47+
* [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments)
48+
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)

0 commit comments

Comments
 (0)