Skip to content

Commit 59e910a

Browse files
Merge pull request #6537 from dotty-staging/add-lets
Add lets as a helper to create bindings
2 parents ffb250d + 0358b5b commit 59e910a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

library/src-3.x/scala/tasty/reflect/utils/TreeUtils.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ trait TreeUtils {
2323
expr.unseal
2424
}
2525

26+
/** Bind the given `terms` to names and use them in the `body` */
27+
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
28+
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
29+
case Nil => body(acc)
30+
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
31+
}
32+
rec(terms, Nil)
33+
}
2634
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object scalatest {
5+
6+
inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition) }
7+
8+
def assertImpl(cond: Expr[Boolean])(implicit refl: Reflection): Expr[Unit] = {
9+
import refl._
10+
import util._
11+
12+
cond.unseal.underlyingArgument match {
13+
case t @ Apply(TypeApply(Select(lhs, op), targs), rhs) =>
14+
let(lhs) { left =>
15+
lets(rhs) { rs =>
16+
val app = Select.overloaded(left, op, targs.map(_.tpe), rs)
17+
val b = app.seal.cast[Boolean]
18+
'{ scala.Predef.assert($b) }.unseal
19+
}
20+
}.seal.cast[Unit]
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
import scalatest._
3+
4+
def main(args: Array[String]): Unit = {
5+
val l = List(3, 5)
6+
assert(l contains 3)
7+
}
8+
}

0 commit comments

Comments
 (0)