@@ -39,16 +39,17 @@ object Matcher {
39
39
40
40
inline def withEnv [T ](env : Env )(body : => given Env => T ): T = body given env
41
41
42
- /** Check that all trees match with =#= and concatenate the results with && */
43
- def (scrutinees : List [Tree ]) =##= (patterns : List [Tree ]) given Env : Matching = {
44
- def rec (l1 : List [Tree ], l2 : List [Tree ]): Matching = (l1, l2) match {
45
- case (x :: xs, y :: ys) => x =#= y && rec(xs, ys)
46
- case (Nil , Nil ) => matched
47
- case _ => notMatched
48
- }
49
- rec(scrutinees, patterns)
42
+ /** Check that all trees match with `mtch` and concatenate the results with && */
43
+ def matchLists [T ](l1 : List [T ], l2 : List [T ])(mtch : (T , T ) => Matching ): Matching = (l1, l2) match {
44
+ case (x :: xs, y :: ys) => mtch(x, y) && matchLists(xs, ys)(mtch)
45
+ case (Nil , Nil ) => matched
46
+ case _ => notMatched
50
47
}
51
48
49
+ /** Check that all trees match with =#= and concatenate the results with && */
50
+ def (scrutinees : List [Tree ]) =##= (patterns : List [Tree ]) given Env : Matching =
51
+ matchLists(scrutinees, patterns)(_ =#= _)
52
+
52
53
/** Check that the trees match and return the contents from the pattern holes.
53
54
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
54
55
*
@@ -178,9 +179,7 @@ object Matcher {
178
179
179
180
case (DefDef (_, typeParams1, paramss1, tpt1, Some (rhs1)), DefDef (_, typeParams2, paramss2, tpt2, Some (rhs2))) =>
180
181
val typeParmasMatch = typeParams1 =##= typeParams2
181
- val paramssMatch =
182
- if (paramss1.size != paramss2.size) notMatched
183
- else foldMatchings(paramss1.zip(paramss2).map { (params1, params2) => params1 =##= params2 }: _* )
182
+ val paramssMatch = matchLists(paramss1, paramss2)(_ =##= _)
184
183
val bindMatch =
185
184
if (hasBindAnnotation(pattern.symbol)) bindingMatch(scrutinee.symbol)
186
185
else matched
@@ -199,16 +198,12 @@ object Matcher {
199
198
200
199
case (Match (scru1, cases1), Match (scru2, cases2)) =>
201
200
val scrutineeMacth = scru1 =#= scru2
202
- val casesMatch =
203
- if (cases1.size != cases2.size) notMatched
204
- else foldMatchings(cases1.zip(cases2).map(caseMatches): _* )
201
+ val casesMatch = matchLists(cases1, cases2)(caseMatches)
205
202
scrutineeMacth && casesMatch
206
203
207
204
case (Try (body1, cases1, finalizer1), Try (body2, cases2, finalizer2)) =>
208
205
val bodyMacth = body1 =#= body2
209
- val casesMatch =
210
- if (cases1.size != cases2.size) notMatched
211
- else foldMatchings(cases1.zip(cases2).map(caseMatches): _* )
206
+ val casesMatch = matchLists(cases1, cases2)(caseMatches)
212
207
val finalizerMatch = treeOptMatches(finalizer1, finalizer2)
213
208
bodyMacth && casesMatch && finalizerMatch
214
209
@@ -282,9 +277,7 @@ object Matcher {
282
277
283
278
case (Pattern .Unapply (fun1, implicits1, patterns1), Pattern .Unapply (fun2, implicits2, patterns2)) =>
284
279
val funMatch = fun1 =#= fun2
285
- val implicitsMatch =
286
- if (implicits1.size != implicits2.size) notMatched
287
- else foldMatchings(implicits1.zip(implicits2).map((i1, i2) => i1 =#= i2): _* )
280
+ val implicitsMatch = implicits1 =##= implicits2
288
281
val (patEnv, patternsMatch) = foldPatterns(patterns1, patterns2)
289
282
(patEnv, funMatch && implicitsMatch && patternsMatch)
290
283
0 commit comments