|
| 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