Skip to content

Commit 89c2c36

Browse files
committed
Add Expr.matches
1 parent c1e0e04 commit 89c2c36

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

library/src/scala/quoted/Expr.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ package quoted {
1919
*/
2020
final def getValue[U >: T](given qctx: QuoteContext, valueOf: ValueOfExpr[U]): Option[U] = valueOf(this)
2121

22+
/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
23+
* It does the equivalent of
24+
* ```
25+
* this match
26+
* case '{...} => true // where the contens of the pattern are the contents of `that`
27+
* case _ => false
28+
* ```
29+
*/
30+
final def matches(that: Expr[Any])(given qctx: QuoteContext): Boolean =
31+
!scala.internal.quoted.Expr.unapply[Unit, Unit](this)(given that, false, qctx).isEmpty
32+
2233
}
2334

2435
object Expr {

tests/run-staging/expr-matches.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
import scala.quoted.staging._
3+
4+
5+
object Test {
6+
given Toolbox = Toolbox.make(getClass.getClassLoader)
7+
def main(args: Array[String]): Unit = withQuoteContext {
8+
assert('{1} matches '{1})
9+
assert('{println("foo")} matches '{println("foo")})
10+
assert('{println("foo")} matches '{println(${Expr("foo")})})
11+
assert('{println(Some("foo"))} matches '{println(${ val a = '{Some("foo")}; a})})
12+
}
13+
}

0 commit comments

Comments
 (0)