forked from rescript-lang/syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspread.res.txt
122 lines (86 loc) · 3.72 KB
/
spread.res.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Syntax error!
tests/parsing/errors/other/spread.res:1:12-14
1 │ let arr = [...x, ...y]
2 │ let [...arr, _] = [1, 2, 3]
3 │
Arrays can't use the `...` spread currently. Please use `concat` or other Array helpers.
Syntax error!
tests/parsing/errors/other/spread.res:2:6-8
1 │ let arr = [...x, ...y]
2 │ let [...arr, _] = [1, 2, 3]
3 │
4 │ let record = {...x, ...y}
Array's `...` spread is not supported in pattern matches.
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
Solution: if it's to validate the first few elements, use a `when` clause + Array size check + `get` checks on the current pattern. If it's to obtain a subarray, use `Array.sub` or `Belt.Array.slice`.
Syntax error!
tests/parsing/errors/other/spread.res:4:21-23
2 │ let [...arr, _] = [1, 2, 3]
3 │
4 │ let record = {...x, ...y}
5 │ let {...x, ...y} = myRecord
6 │
Records can only have one `...` spread, at the beginning.
Explanation: since records have a known, fixed shape, a spread like `{a, ...b}` wouldn't make sense, as `b` would override every field of `a` anyway.
Syntax error!
tests/parsing/errors/other/spread.res:5:15-18
3 │
4 │ let record = {...x, ...y}
5 │ let {...x, ...y} = myRecord
6 │
7 │ let myList = list{...x, ...y}
Record's `...` spread is not supported in pattern matches.
Explanation: you can't collect a subset of a record's field into its own record, since a record needs an explicit declaration and that subset wouldn't have one.
Solution: you need to pull out each field you want explicitly.
Syntax error!
tests/parsing/errors/other/spread.res:8:1-3
6 │
7 │ let myList = list{...x, ...y}
8 │ let list{...x, ...y} = myList
9 │
10 │ type t = {...a}
Lists can only have one `...` spread, and at the end.
Explanation: lists are singly-linked list, where a node contains a value and points to the next node. `list{a, ...bc}` efficiently creates a new item and links `bc` as its next nodes. `list{...bc, a}` would be expensive, as it'd need to traverse `bc` and prepend each item to `a` one by one. We therefore disallow such syntax sugar.
Solution: directly use `concat`.
Syntax error!
tests/parsing/errors/other/spread.res:8:13-22
6 │
7 │ let myList = list{...x, ...y}
8 │ let list{...x, ...y} = myList
9 │
10 │ type t = {...a}
List pattern matches only supports one `...` spread, at the end.
Explanation: a list spread at the tail is efficient, but a spread in the middle would create new lists; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
Syntax error!
tests/parsing/errors/other/spread.res:10:11-13
8 │ let list{...x, ...y} = myList
9 │
10 │ type t = {...a}
11 │ type t = Foo({...a})
12 │ type t = option<foo, {...x}>
You're using a ... spread without extra fields. This is the same type.
Syntax error!
tests/parsing/errors/other/spread.res:11:15-17
9 │
10 │ type t = {...a}
11 │ type t = Foo({...a})
12 │ type t = option<foo, {...x}>
13 │
You're using a ... spread without extra fields. This is the same type.
Syntax error!
tests/parsing/errors/other/spread.res:12:23-26
10 │ type t = {...a}
11 │ type t = Foo({...a})
12 │ type t = option<foo, {...x}>
13 │
You're using a ... spread without extra fields. This is the same type.
let arr = [|x;y|]
let [|arr;_|] = [|1;2;3|]
let record = { x with y }
let { x; y } = myRecord
let myList = x :: y
let x::y = myList
type nonrec t = < a >
type nonrec t =
| Foo of < a >
type nonrec t = (foo, < x > ) option