Skip to content

Commit 6574ca9

Browse files
committed
polyvariants pattern matching spread example
1 parent cd1622b commit 6574ca9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

misc_docs/syntax/language_spreads.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ let isPet = (animal: animals) => {
7373

7474
Read more about [variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants).
7575

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+
7698
## List immutable update
7799
Spreads can be used for immutable updates of lists:
78100

@@ -133,6 +155,7 @@ Read more about [partial application of functions](function.md#partial-applicati
133155
* [Record immutable updates](record.md#immutable-update)
134156
* [Variant type spreads](variant.md#variant-type-spreads)
135157
* [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)
136159
* [List immutable updates](array-and-list.md#immutable-prepend)
137160
* [List pattern matching](pattern-matching-destructuring.md#match-on-list)
138161
* [Array spreads](array-and-list.md#array-spreads)

0 commit comments

Comments
 (0)