@@ -39,139 +39,143 @@ test("")
39
39
test(12)
40
40
//│ ═══[RUNTIME ERROR] Error: match error
41
41
42
- :ucs desugared
43
42
class Point(x: Int, y: Int)
44
- fun discarded_cases(thing) =
43
+
44
+ :ucs desugared
45
+ fun with_other_constructors(thing) =
45
46
if thing is
46
- [x, y] then x + y
47
+ [x, y] then x * y
47
48
Point(x, y) then x + y
48
49
//│ Desugared:
49
50
//│ > if
50
- //│ > thing@40 is []=2 and
51
- //│ > let $first0@47 = thing@40#2.0
52
- //│ > let $first1@46 = thing@40#3.1
53
- //│ > let x@48 = $first0@47#0
54
- //│ > let y@49 = $first1@46#0
55
- //│ > else builtin:+#82(x@48#666, y@49#666)
56
- //│ > thing@40 is Point($param0@41, $param1@42) and
57
- //│ > let x@43 = $param0@41#0
58
- //│ > let y@44 = $param1@42#0
59
- //│ > else builtin:+#81(x@43#666, y@44#666)
60
-
61
- :e
62
- :todo
63
- discarded_cases(Point(0, 0))
64
- //│ = 0
51
+ //│ > thing@41 is []=2 and
52
+ //│ > let $first0@48 = thing@41#2.0
53
+ //│ > let $first1@47 = thing@41#3.1
54
+ //│ > let x@49 = $first0@48#0
55
+ //│ > let y@50 = $first1@47#0
56
+ //│ > else builtin:*#23(x@49#666, y@50#666)
57
+ //│ > thing@41 is Point($param0@42, $param1@43) and
58
+ //│ > let x@44 = $param0@42#0
59
+ //│ > let y@45 = $param1@43#0
60
+ //│ > else builtin:+#81(x@44#666, y@45#666)
61
+
62
+ :expect 7
63
+ with_other_constructors(Point(3, 4))
64
+ //│ = 7
65
+
66
+ :expect 12
67
+ with_other_constructors([3, 4])
68
+ //│ = 12
65
69
66
70
// A workaround is to move the tuple pattern to the last case.
67
71
:ucs desugared
68
- fun working_cases (thing) =
72
+ fun with_other_constructors (thing) =
69
73
if thing is
70
74
Point(x, y) then x + y
71
- [x, y] then x + y
75
+ [x, y] then x * y
72
76
//│ Desugared:
73
77
//│ > if
74
- //│ > thing@61 is Point($param0@67 , $param1@68 ) and
75
- //│ > let x@69 = $param0@67 #0
76
- //│ > let y@70 = $param1@68 #0
77
- //│ > else builtin:+#84 (x@69 #666, y@70 #666)
78
- //│ > thing@61 is []=2 and
79
- //│ > let $first0@63 = thing@61 #1.0
80
- //│ > let $first1@62 = thing@61 #2.1
81
- //│ > let x@64 = $first0@63 #0
82
- //│ > let y@65 = $first1@62 #0
83
- //│ > else builtin:+#83 (x@64 #666, y@65 #666)
84
-
85
- working_cases(Point(0, 0))
86
- //│ = 0
87
-
88
- // However, the `Object` type forbids tuples to be used.
89
- :todo
90
- working_cases([0, 0 ])
91
- //│ = 0
92
-
93
-
94
- fun not_working (x) =
78
+ //│ > thing@65 is Point($param0@71 , $param1@72 ) and
79
+ //│ > let x@73 = $param0@71 #0
80
+ //│ > let y@74 = $param1@72 #0
81
+ //│ > else builtin:+#82 (x@73 #666, y@74 #666)
82
+ //│ > thing@65 is []=2 and
83
+ //│ > let $first0@67 = thing@65 #1.0
84
+ //│ > let $first1@66 = thing@65 #2.1
85
+ //│ > let x@68 = $first0@67 #0
86
+ //│ > let y@69 = $first1@66 #0
87
+ //│ > else builtin:*#24 (x@68 #666, y@69 #666)
88
+
89
+ :expect 7
90
+ with_other_constructors(Point(3, 4))
91
+ //│ = 7
92
+
93
+ :expect 12
94
+ with_other_constructors([3, 4 ])
95
+ //│ = 12
96
+
97
+
98
+ fun with_else (x) =
95
99
if x is
96
100
[a, b, c] then
97
101
a + b + c
98
102
else
99
103
0
100
104
101
- not_working ([1, 2, 3])
105
+ with_else ([1, 2, 3])
102
106
//│ = 6
103
107
104
- not_working ([1, 2])
108
+ with_else ([1, 2])
105
109
//│ = 0
106
110
107
111
:ucs desugared
108
- fun multiple_checks (xs) =
112
+ fun match_against_different_length (xs) =
109
113
if xs is
110
114
[] then 0
111
115
[x] then x + 1
112
116
[x, y] then x + y + 2
113
117
[x, y, z] then x + y + z + 3
114
118
//│ Desugared:
115
119
//│ > if
116
- //│ > xs@105 is []=0 then 0
117
- //│ > xs@105 is []=1 and
118
- //│ > let $first0@108 = xs@105 #8.0
119
- //│ > let x@119 = $first0@108 #2
120
- //│ > else builtin:+#92 (x@119 #666, 1)
121
- //│ > xs@105 is []=2 and
122
- //│ > let $first0@108 = xs@105 #5.0
123
- //│ > let $first1@107 = xs@105 #6.1
124
- //│ > let x@115 = $first0@108 #1
125
- //│ > let y@116 = $first1@107 #1
126
- //│ > else builtin:+#90 (builtin:+#91 (x@115 #666, y@116 #666), 2)
127
- //│ > xs@105 is []=3 and
128
- //│ > let $first0@108 = xs@105 #1.0
129
- //│ > let $first1@107 = xs@105 #2.1
130
- //│ > let $first2@106 = xs@105 #3.2
131
- //│ > let x@109 = $first0@108 #0
132
- //│ > let y@110 = $first1@107 #0
133
- //│ > let z@111 = $first2@106 #0
134
- //│ > else builtin:+#87 (builtin:+#88 (builtin:+#89 (x@109 #666, y@110 #666), z@111 #666), 3)
120
+ //│ > xs@109 is []=0 then 0
121
+ //│ > xs@109 is []=1 and
122
+ //│ > let $first0@112 = xs@109 #8.0
123
+ //│ > let x@123 = $first0@112 #2
124
+ //│ > else builtin:+#90 (x@123 #666, 1)
125
+ //│ > xs@109 is []=2 and
126
+ //│ > let $first0@112 = xs@109 #5.0
127
+ //│ > let $first1@111 = xs@109 #6.1
128
+ //│ > let x@119 = $first0@112 #1
129
+ //│ > let y@120 = $first1@111 #1
130
+ //│ > else builtin:+#88 (builtin:+#89 (x@119 #666, y@120 #666), 2)
131
+ //│ > xs@109 is []=3 and
132
+ //│ > let $first0@112 = xs@109 #1.0
133
+ //│ > let $first1@111 = xs@109 #2.1
134
+ //│ > let $first2@110 = xs@109 #3.2
135
+ //│ > let x@113 = $first0@112 #0
136
+ //│ > let y@114 = $first1@111 #0
137
+ //│ > let z@115 = $first2@110 #0
138
+ //│ > else builtin:+#85 (builtin:+#86 (builtin:+#87 (x@113 #666, y@114 #666), z@115 #666), 3)
135
139
136
140
:expect 0
137
- multiple_checks ([])
141
+ match_against_different_length ([])
138
142
//│ = 0
139
143
140
144
:expect 18
141
- multiple_checks ([17])
145
+ match_against_different_length ([17])
142
146
//│ = 18
143
147
144
148
:expect 22
145
- multiple_checks ([9, 11])
149
+ match_against_different_length ([9, 11])
146
150
//│ = 22
147
151
148
152
:expect 42
149
- multiple_checks ([13, 13, 13])
153
+ match_against_different_length ([13, 13, 13])
150
154
//│ = 42
151
155
152
156
:import ../Prelude/Option.mls
153
157
//│ Imported 3 member(s)
154
158
155
159
:ucs desugared normalized
156
160
:todo // Sub-scrutinees are not memorized thus the normalization is not correct.
157
- fun multiple_checks (xs) =
161
+ fun with_the_common_prefix (xs) =
158
162
if xs is
159
163
[Some(x)] then x + 1
160
164
[None] then 0
161
165
//│ Desugared:
162
166
//│ > if
163
- //│ > xs@147 is []=1 and
164
- //│ > let $first0@148 = xs@147 #3.0
165
- //│ > $first0@148 is Some($param0@149 ) and
166
- //│ > let x@150 = $param0@149 #0
167
- //│ > else builtin:+#93 (x@150 #666, 1)
168
- //│ > xs@147 is []=1 and
169
- //│ > let $first0@148 = xs@147 #1.0
170
- //│ > $first0@148 is None then 0
167
+ //│ > xs@151 is []=1 and
168
+ //│ > let $first0@152 = xs@151 #3.0
169
+ //│ > $first0@152 is Some($param0@153 ) and
170
+ //│ > let x@154 = $param0@153 #0
171
+ //│ > else builtin:+#91 (x@154 #666, 1)
172
+ //│ > xs@151 is []=1 and
173
+ //│ > let $first0@152 = xs@151 #1.0
174
+ //│ > $first0@152 is None then 0
171
175
//│ Normalized:
172
- //│ > if xs@147 is []=1 and
173
- //│ > let $first0@148 = xs@147 #3.0
174
- //│ > $first0@148 is Some($param0@149 ) and
175
- //│ > let x@150 = $param0@149 #0
176
- //│ > else builtin:+#93 (x@150 #666, 1)
177
- //│ > $first0@148 is None then 0
176
+ //│ > if xs@151 is []=1 and
177
+ //│ > let $first0@152 = xs@151 #3.0
178
+ //│ > $first0@152 is Some($param0@153 ) and
179
+ //│ > let x@154 = $param0@153 #0
180
+ //│ > else builtin:+#91 (x@154 #666, 1)
181
+ //│ > $first0@152 is None then 0
0 commit comments