Skip to content

Commit bb7ab37

Browse files
committed
Merge branch 'master' of https://github.com/rescript-lang/rescript-lang.org into vlk-v12-doc-restructure
2 parents e0db4d8 + fea5395 commit bb7ab37

File tree

3 files changed

+238
-1
lines changed

3 files changed

+238
-1
lines changed

misc_docs/syntax/decorator_as.mdx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ summary: "This is the `@as` decorator."
66
category: "decorators"
77
---
88

9-
The `@as` decorator is commonly used on record types to alias record field names to a different JavaScript attribute name.
9+
The `@as` decorator has multiple uses in ReScript.
10+
11+
## Change runtime name of record field
12+
The `@as` decorator can be used on record types to alias record field names to a different JavaScript attribute name.
1013

1114
This is useful to map to JavaScript attribute names that cannot be expressed in ReScript (such as keywords).
1215

@@ -52,8 +55,36 @@ var value = [
5255

5356
</CodeTab>
5457

58+
## Change the runtime representation of a variant constructor
59+
Similarily to changing the runtime name of a record field, you can change the runtime representation of a variant constructor using `@as()`. Only with variants, you have many more options for the runtime representation than for record field names:
60+
61+
<CodeTab labels={["ReScript", "JS Output"]}>
62+
63+
```res
64+
@unboxed
65+
type pet = | @as("dog") Dog | @as(1) Cat | @as(null) SomethingElse
66+
67+
let dog = Dog
68+
let cat = Cat
69+
let somethingElse = SomethingElse
70+
71+
```
72+
73+
```js
74+
let dog = "dog";
75+
76+
let cat = 1;
77+
78+
let somethingElse = null;
79+
```
80+
81+
</CodeTab>
82+
83+
Read more about the [`@as` decorator and variants](variant.md#valid-as-payloads).
84+
5585
### References
5686

5787
* [Bind Using ReScript Record](/docs/manual/latest/bind-to-js-object#bind-using-rescript-record)
5888
* [Constrain Arguments Better](/docs/manual/latest/bind-to-js-function#constrain-arguments-better)
5989
* [Fixed Arguments](/docs/manual/latest/bind-to-js-function#fixed-arguments)
90+
* [`@as` decorator and variants](variant.md#valid-as-payloads)

misc_docs/syntax/decorator_tag.mdx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
id: "tag-decorator"
3+
keywords: ["tag", "decorator"]
4+
name: "@tag"
5+
summary: "This is the `@tag` decorator."
6+
category: "decorators"
7+
---
8+
9+
The `@tag` decorator is used to customize the discriminator for tagged variants.
10+
11+
<CodeTab labels={["ReScript", "JS Output"]}>
12+
13+
```res
14+
type mood = Happy({level: int}) | Sad({level: int})
15+
16+
@tag("kind")
17+
type mood2 = Happy({level: int}) | Sad({level: int})
18+
19+
let mood: mood = Happy({level: 10})
20+
let mood2: mood2 = Happy({level: 11})
21+
22+
```
23+
24+
```js
25+
let mood = {
26+
TAG: "Happy",
27+
level: 10
28+
};
29+
30+
let mood2 = {
31+
kind: "Happy",
32+
level: 11
33+
};
34+
```
35+
36+
</CodeTab>
37+
38+
Notice the different discriminators in the JS output: `TAG` in `mood` vs `kind` in `mood2`.
39+
40+
Read more about [using `@tag` with variants](variant.md#tagged-variants).
41+
42+
### References
43+
44+
* [Using `@tag` with variants](variant.md#tagged-variants)

misc_docs/syntax/language_spreads.mdx

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
id: "spreads"
3+
keywords: ["spread", "record", "variant", "polymorphic", "array", "list", "function", "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+
## Polymorphic variant pattern matching
77+
You can refine compatible polymorphic variants when pattern matching:
78+
79+
```res
80+
type red = [#Ruby | #Redwood | #Rust]
81+
type blue = [#Sapphire | #Neon | #Navy]
82+
83+
// Contains all constructors of red and blue.
84+
// Also adds #Papayawhip
85+
type color = [red | blue | #Papayawhip]
86+
87+
let myColor: color = #Ruby
88+
89+
switch myColor {
90+
| #...blue => Console.log("This blue-ish")
91+
| #...red => Console.log("This red-ish")
92+
| other => Console.log2("Other color than red and blue: ", other)
93+
}
94+
```
95+
96+
Read more about [pattern matching and polymorphic variants](polymorphic-variant.md#combine-types-and-pattern-match).
97+
98+
## List immutable update
99+
Spreads can be used for immutable updates of lists:
100+
101+
```res
102+
let prepended = list{1, ...someOtherList}
103+
104+
// You can spread several lists, but it's O(n) so avoid it if possible
105+
let multiple = list{1, ...prepended, ...anotherList}
106+
```
107+
108+
Read more about [immutable list updates](array-and-list.md#immutable-prepend) here.
109+
110+
## List pattern matching
111+
Spreads can be used when pattern matching on lists:
112+
113+
```res
114+
let rec printStudents = (students) => {
115+
switch students {
116+
| list{} => () // done
117+
| list{student} => Console.log("Last student: " ++ student)
118+
| list{student1, ...otherStudents} =>
119+
Console.log(student1)
120+
printStudents(otherStudents)
121+
}
122+
}
123+
printStudents(list{"Jane", "Harvey", "Patrick"})
124+
```
125+
126+
Read more about [pattern matching on lists](pattern-matching-destructuring.md#match-on-list).
127+
128+
## Array immutable update
129+
> Available in v11+
130+
131+
You can use spreads to add the contents of one array to another, just like in JavaScript:
132+
133+
```res
134+
let firstArray = [1, 2, 3]
135+
let secondArray = [...firstArray, 4, 5]
136+
```
137+
138+
Read more about [array spreads](array-and-list.md#array-spreads).
139+
140+
## Partial application of functions
141+
> Available in v11+ (uncurried mode)
142+
143+
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.
144+
145+
```res
146+
let add = (a, b) => a + b
147+
let addFive = add(5, ...)
148+
```
149+
150+
Read more about [partial application of functions](function.md#partial-application).
151+
152+
### References
153+
154+
* [Record type spreads](record.md#record-type-spread)
155+
* [Record immutable updates](record.md#immutable-update)
156+
* [Variant type spreads](variant.md#variant-type-spreads)
157+
* [Variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants)
158+
* [Pattern matching and polymorphic variants](polymorphic-variant.md#combine-types-and-pattern-match)
159+
* [List immutable updates](array-and-list.md#immutable-prepend)
160+
* [List pattern matching](pattern-matching-destructuring.md#match-on-list)
161+
* [Array spreads](array-and-list.md#array-spreads)
162+
* [Partial application of functions](function.md#partial-application)

0 commit comments

Comments
 (0)