Skip to content

Syntax lookup: labeled and optional args #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions misc_docs/syntax/decorator_send_pipe.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ keywords: ["send", "pipe", "decorator"]
name: "@bs.send.pipe"
summary: "This is the `@bs.send.pipe` decorator."
category: "decorators"
status: "deprecated"
---

> Removed since compiler version 10.0. Use the [@send](https://rescript-lang.org/docs/manual/latest/bind-to-js-function) decorator instead.
Expand Down
36 changes: 36 additions & 0 deletions misc_docs/syntax/language_labeled_argument.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
id: "labeled-argument"
keywords: ["labeled", "argument"]
name: "~arg"
summary: "This is a `labeled argument`."
category: "languageconstructs"
---

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.

### Example

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

```res
let calculateDistance = (~x1, ~y1, ~x2, ~y2) => {
Math.sqrt((x1 -. x2) ** 2. +. (y1 -. y2) ** 2.)
}

calculateDistance(~x1=6., ~y1=8., ~x2=3., ~y2=4.)
```

```js
function calculateDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

calculateDistance(6, 8, 3, 4);
```

</CodeTab>

### References

* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)
48 changes: 48 additions & 0 deletions misc_docs/syntax/language_optional_labeled_argument.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
id: "optional-labeled-argument"
keywords: ["optional", "labeled", "argument"]
name: "~arg=?"
summary: "This is an `optional labeled argument`."
category: "languageconstructs"
---

Labeled arguments, i.e. arguments that are prefixed with `~`, can be suffixed with `=?` to denote that they are optional. Thus, they can be
omitted when calling the function.

### Example

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

```res
let print = (text, ~logLevel=?) => {
switch logLevel {
| Some(#error) => Console.error(text)
| _ => Console.log(text)
}
}

print("An info")
print("An error", ~logLevel=#error)
```

```js
function print(text, logLevel) {
if (logLevel === "error") {
console.error(text);
} else {
console.log(text);
}
}

print("An info", undefined);

print("An error", "error");
```

</CodeTab>

### References

* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments)
* [Optional Labeled Arguments](/docs/manual/latest/function#optional-labeled-arguments)
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks)