Skip to content

Commit cc5fbee

Browse files
Syntax lookup: labeled and optional args (#888)
* Syntax Lookup: Add labeled args * Syntax Lookup: Add optional args * Syntax Lookup: Make send.pipe deprecated * Syntax Lookup: Default labeled args * Syntax Lookup: Mention partial application and order of labeled args
1 parent b8a4eed commit cc5fbee

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

misc_docs/syntax/decorator_send_pipe.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ keywords: ["send", "pipe", "decorator"]
44
name: "@bs.send.pipe"
55
summary: "This is the `@bs.send.pipe` decorator."
66
category: "decorators"
7+
status: "deprecated"
78
---
89

910
> Removed since compiler version 10.0. Use the [@send](https://rescript-lang.org/docs/manual/latest/bind-to-js-function) decorator instead.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: "labeled-argument"
3+
keywords: ["labeled", "argument"]
4+
name: "~arg"
5+
summary: "This is a `labeled argument`."
6+
category: "languageconstructs"
7+
---
8+
9+
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.
10+
11+
### Example
12+
13+
<CodeTab labels={["ReScript", "JS Output"]}>
14+
15+
```res prelude
16+
let calculateDistance = (~x1, ~y1, ~x2, ~y2) => {
17+
Math.sqrt((x1 -. x2) ** 2. +. (y1 -. y2) ** 2.)
18+
}
19+
20+
calculateDistance(~x1=6., ~y1=8., ~x2=3., ~y2=4.)
21+
```
22+
23+
```js
24+
function calculateDistance(x1, y1, x2, y2) {
25+
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
26+
}
27+
28+
calculateDistance(6, 8, 3, 4);
29+
```
30+
31+
</CodeTab>
32+
33+
Labeled arguments can be provided in any order:
34+
35+
<CodeTab labels={["ReScript", "JS Output"]}>
36+
37+
```res example
38+
calculateDistance(~x1=6., ~x2=3., ~y1=8., ~y2=4.)
39+
```
40+
41+
```js
42+
calculateDistance(6, 8, 3, 4);
43+
```
44+
45+
</CodeTab>
46+
47+
This also works together with partial application:
48+
49+
<CodeTab labels={["ReScript", "JS Output"]}>
50+
51+
```res example
52+
let calcY = calculateDistance(~x1=6., ~x2=3., ...)
53+
calcY(~y1=8., ~y2=4.)
54+
```
55+
56+
```js
57+
function calcY(none, extra) {
58+
return calculateDistance(6, none, 3, extra);
59+
}
60+
61+
calcY(8, 4);
62+
```
63+
64+
</CodeTab>
65+
66+
### References
67+
68+
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
69+
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 example
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+
Optional labeled arguments can also hold a default value.
45+
46+
<CodeTab labels={["ReScript", "JS Output"]}>
47+
48+
```res example
49+
let print = (text, ~logLevel=#info) => {
50+
switch logLevel {
51+
| #error => Console.error(text)
52+
| #warn => Console.warn(text)
53+
| #info => Console.log(text)
54+
}
55+
}
56+
57+
print("An info")
58+
print("A warning", ~logLevel=#warn)
59+
```
60+
61+
```js
62+
function print(text, logLevelOpt) {
63+
var logLevel = logLevelOpt !== undefined ? logLevelOpt : "info";
64+
if (logLevel === "warn") {
65+
console.warn(text);
66+
} else if (logLevel === "error") {
67+
console.error(text);
68+
} else {
69+
console.log(text);
70+
}
71+
}
72+
73+
print("An info", undefined);
74+
75+
print("A warning", "warn");
76+
```
77+
78+
</CodeTab>
79+
80+
### References
81+
82+
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
83+
* [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments)
84+
* [Labeled Argument with Default Value](/docs/manual/latest/function#optional-with-default-value)
85+
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)

0 commit comments

Comments
 (0)