Skip to content

Commit c9e4a4b

Browse files
Syntax Lookup: Add labeled args
1 parent b860b4f commit c9e4a4b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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
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+
### References
34+
35+
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
36+
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)

0 commit comments

Comments
 (0)