@@ -31,12 +31,13 @@ object Matcher {
31
31
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]``
32
32
*/
33
33
def unapply [Tup <: Tuple ](scrutineeExpr : Expr [_])(implicit patternExpr : Expr [_], reflection : Reflection ): Option [Tup ] = {
34
+ // TODO improve performance
34
35
import reflection .{Bind => BindPattern , _ }
35
36
import Matching ._
36
37
37
38
type Env = Set [(Symbol , Symbol )]
38
39
39
- // TODO improve performance
40
+ inline def withEnv [ T ]( env : Env )( body : given Env => T ) : T = body given env
40
41
41
42
/** Check that the trees match and return the contents from the pattern holes.
42
43
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
@@ -120,10 +121,7 @@ object Matcher {
120
121
fn1 =#= fn2 && args1 =##= args2
121
122
122
123
case (Block (stats1, expr1), Block (stats2, expr2)) =>
123
- val env = the[Env ] ++ stats1.map(_.symbol).zip(stats2.map(_.symbol))
124
-
125
- {
126
- implied for Env = env
124
+ withEnv(the[Env ] ++ stats1.map(_.symbol).zip(stats2.map(_.symbol))) {
127
125
stats1 =##= stats2 && expr1 =#= expr2
128
126
}
129
127
@@ -247,9 +245,7 @@ object Matcher {
247
245
248
246
def caseMatches (scrutinee : CaseDef , pattern : CaseDef ) given Env : Matching = {
249
247
val (caseEnv, patternMatch) = scrutinee.pattern =%= pattern.pattern
250
-
251
- {
252
- implied for Env = caseEnv
248
+ withEnv(caseEnv) {
253
249
val guardMatch = treeOptMatches(scrutinee.guard, pattern.guard)
254
250
val rhsMatch = scrutinee.rhs =#= pattern.rhs
255
251
patternMatch && guardMatch && rhsMatch
0 commit comments