-
-
Notifications
You must be signed in to change notification settings - Fork 251
Document spreads in the syntax lookup #1009
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 1 commit
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
id: "spreads" | ||
keywords: ["spread", "record", "variant", "partial"] | ||
name: "..." | ||
summary: "This is the `...` syntax." | ||
category: "languageconstructs" | ||
--- | ||
|
||
A `spread` is three dots in a row: `...`. Spreads have many different uses in ReScript depending on in which context it is used. | ||
## Record definitions | ||
> Available in v10+ | ||
|
||
Spreads can be used to copy fields from one record definition into another. | ||
|
||
```res | ||
type a = { | ||
id: string, | ||
name: string, | ||
} | ||
|
||
type b = { | ||
age: int | ||
} | ||
|
||
type c = { | ||
...a, | ||
...b, | ||
active: bool | ||
} | ||
``` | ||
|
||
Read more about [record type spreads here](record.md#record-type-spread). | ||
|
||
## Record immutable update | ||
Spreads can be used for immutable updates of records: | ||
|
||
```res | ||
let meNextYear = {...me, age: me.age + 1} | ||
``` | ||
|
||
Read more about [record immutable updates here](record.md#immutable-update). | ||
|
||
## Variant definitions | ||
> Available in v11+ | ||
|
||
Spreads can be used to copy constructors from one variant definition to another. | ||
|
||
```res | ||
type a = One | Two | Three | ||
type b = | ...a | Four | Five | ||
// b is now One | Two | Three | Four | Five | ||
``` | ||
|
||
Read more about [variant type spreads](variant.md#variant-type-spreads) here. | ||
|
||
## Variant pattern matching | ||
> Available in v12+ | ||
|
||
You can refine the type of a variant by spreading compatible a variant when pattern matching: | ||
```res | ||
type pets = Cat | Dog | ||
type fish = Cod | Salmon | ||
type animals = | ...pets | ...fish | ||
|
||
let isPet = (animal: animals) => { | ||
switch animal { | ||
| ...dog => Console.log("A dog!") | ||
| _ => Console.log("Not a dog...") | ||
} | ||
} | ||
|
||
``` | ||
|
||
Read more about [variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants). | ||
|
||
## Partial application of functions | ||
> Available in v11+ (uncurried mode) | ||
|
||
You can partially apply a function using the spread syntax. Partially applying a function will return a new function taking only the arguments that wasn't applied partially. | ||
|
||
```res | ||
let add = (a, b) => a + b | ||
let addFive = add(5, ...) | ||
``` | ||
|
||
Read more about [partial application of functions](function.md#partial-application). | ||
|
||
### References | ||
|
||
* [Record type spreads](record.md#record-type-spread) | ||
* [Record immutable updates](record.md#immutable-update) | ||
* [Variant type spreads](variant.md#variant-type-spreads) | ||
* [Variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants) | ||
* [Partial application of functions](function.md#partial-application) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the keywords.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine to me