Skip to content

Add lets as a helper to create bindings #6537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/src-3.x/scala/tasty/reflect/utils/TreeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ trait TreeUtils {
expr.unseal
}

/** Bind the given `terms` to names and use them in the `body` */
def lets(terms: List[Term])(body: List[Term] => Term): Term = {
def rec(xs: List[Term], acc: List[Term]): Term = xs match {
case Nil => body(acc)
case x :: xs => let(x) { (x: Term) => rec(xs, x :: acc) }
}
rec(terms, Nil)
}
}
23 changes: 23 additions & 0 deletions tests/run-macros/reflect-pos-fun/assert_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.quoted._
import scala.tasty._

object scalatest {

inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition) }

def assertImpl(cond: Expr[Boolean])(implicit refl: Reflection): Expr[Unit] = {
import refl._
import util._

cond.unseal.underlyingArgument match {
case t @ Apply(TypeApply(Select(lhs, op), targs), rhs) =>
let(lhs) { left =>
lets(rhs) { rs =>
val app = Select.overloaded(left, op, targs.map(_.tpe), rs)
val b = app.seal.cast[Boolean]
'{ scala.Predef.assert($b) }.unseal
}
}.seal.cast[Unit]
}
}
}
8 changes: 8 additions & 0 deletions tests/run-macros/reflect-pos-fun/test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
import scalatest._

def main(args: Array[String]): Unit = {
val l = List(3, 5)
assert(l contains 3)
}
}