-
-
Notifications
You must be signed in to change notification settings - Fork 251
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9e4a4b
Syntax Lookup: Add labeled args
fhammerschmidt 210a908
Syntax Lookup: Add optional args
fhammerschmidt a974e65
Syntax Lookup: Make send.pipe deprecated
fhammerschmidt 82640a9
Syntax Lookup: Default labeled args
fhammerschmidt 22a4b9a
Syntax Lookup: Mention partial application and order of labeled args
fhammerschmidt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
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 prelude | ||
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> | ||
|
||
Labeled arguments can be provided in any order: | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res example | ||
calculateDistance(~x1=6., ~x2=3., ~y1=8., ~y2=4.) | ||
``` | ||
|
||
```js | ||
calculateDistance(6, 8, 3, 4); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
This also works together with partial application: | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res example | ||
let calcY = calculateDistance(~x1=6., ~x2=3., ...) | ||
calcY(~y1=8., ~y2=4.) | ||
``` | ||
|
||
```js | ||
function calcY(none, extra) { | ||
return calculateDistance(6, none, 3, extra); | ||
} | ||
|
||
calcY(8, 4); | ||
``` | ||
|
||
</CodeTab> | ||
|
||
### References | ||
|
||
* [Labeled Arguments](/docs/manual/latest/function#labeled-arguments) | ||
* [Function Syntax Cheatsheet](/docs/manual/latest/function#tips--tricks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
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 | ||
fhammerschmidt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
omitted when calling the function. | ||
|
||
### Example | ||
|
||
<CodeTab labels={["ReScript", "JS Output"]}> | ||
|
||
```res example | ||
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> | ||
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.