Skip to content

Commit ac81889

Browse files
committed
document spreads in the syntax lookup
1 parent 97a3b52 commit ac81889

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

misc_docs/syntax/language_spreads.mdx

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
id: "spreads"
3+
keywords: ["spread", "record", "variant", "partial"]
4+
name: "..."
5+
summary: "This is the `...` syntax."
6+
category: "languageconstructs"
7+
---
8+
9+
A `spread` is three dots in a row: `...`. Spreads have many different uses in ReScript depending on in which context it is used.
10+
## Record definitions
11+
> Available in v10+
12+
13+
Spreads can be used to copy fields from one record definition into another.
14+
15+
```res
16+
type a = {
17+
id: string,
18+
name: string,
19+
}
20+
21+
type b = {
22+
age: int
23+
}
24+
25+
type c = {
26+
...a,
27+
...b,
28+
active: bool
29+
}
30+
```
31+
32+
Read more about [record type spreads here](record.md#record-type-spread).
33+
34+
## Record immutable update
35+
Spreads can be used for immutable updates of records:
36+
37+
```res
38+
let meNextYear = {...me, age: me.age + 1}
39+
```
40+
41+
Read more about [record immutable updates here](record.md#immutable-update).
42+
43+
## Variant definitions
44+
> Available in v11+
45+
46+
Spreads can be used to copy constructors from one variant definition to another.
47+
48+
```res
49+
type a = One | Two | Three
50+
type b = | ...a | Four | Five
51+
// b is now One | Two | Three | Four | Five
52+
```
53+
54+
Read more about [variant type spreads](variant.md#variant-type-spreads) here.
55+
56+
## Variant pattern matching
57+
> Available in v12+
58+
59+
You can refine the type of a variant by spreading compatible a variant when pattern matching:
60+
```res
61+
type pets = Cat | Dog
62+
type fish = Cod | Salmon
63+
type animals = | ...pets | ...fish
64+
65+
let isPet = (animal: animals) => {
66+
switch animal {
67+
| ...dog => Console.log("A dog!")
68+
| _ => Console.log("Not a dog...")
69+
}
70+
}
71+
72+
```
73+
74+
Read more about [variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants).
75+
76+
## Partial application of functions
77+
> Available in v11+ (uncurried mode)
78+
79+
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.
80+
81+
```res
82+
let add = (a, b) => a + b
83+
let addFive = add(5, ...)
84+
```
85+
86+
Read more about [partial application of functions](function.md#partial-application).
87+
88+
### References
89+
90+
* [Record type spreads](record.md#record-type-spread)
91+
* [Record immutable updates](record.md#immutable-update)
92+
* [Variant type spreads](variant.md#variant-type-spreads)
93+
* [Variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants)
94+
* [Partial application of functions](function.md#partial-application)
95+

0 commit comments

Comments
 (0)