File tree Expand file tree Collapse file tree 4 files changed +27
-42
lines changed
tests/run-with-compiler/tasty-unsafe-let Expand file tree Collapse file tree 4 files changed +27
-42
lines changed Original file line number Diff line number Diff line change @@ -40,9 +40,4 @@ class Reflection(val kernel: Kernel)
4040 def typeChecks (code : String )(implicit ctx : Context ): Boolean = kernel.typeChecks(code)(ctx)
4141 }
4242
43- // TODO integrate with TreeUtils
44- val util : reflect.utils.TreeUtils { val reflect : self.type } = new reflect.utils.TreeUtils {
45- val reflect : self.type = self
46- }
47-
4843}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ trait TreeUtils
66 extends Core
77 with PatternOps
88 with SymbolOps
9- with TreeOps {
9+ with TreeOps { self : Reflection =>
1010
1111 abstract class TreeAccumulator [X ] {
1212
@@ -283,4 +283,29 @@ trait TreeUtils
283283
284284 }
285285
286+ /** Bind the `rhs` to a `val` and use it in `body` */
287+ def let (rhs : Term )(body : Ident => Term ): Term = {
288+ import scala .quoted .QuoteContext
289+ delegate for QuoteContext = new QuoteContext (this )
290+ type T // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
291+ implicit val rhsTpe : quoted.Type [T ] = rhs.tpe.seal.asInstanceOf [quoted.Type [T ]]
292+ val rhsExpr = rhs.seal.cast[T ]
293+ val expr = ' {
294+ val x = $rhsExpr
295+ $ {
296+ val id = (' x ).unseal.asInstanceOf [Ident ]
297+ body(id).seal
298+ }
299+ }
300+ expr.unseal
301+ }
302+
303+ /** Bind the given `terms` to names and use them in the `body` */
304+ def lets (terms : List [Term ])(body : List [Term ] => Term ): Term = {
305+ def rec (xs : List [Term ], acc : List [Term ]): Term = xs match {
306+ case Nil => body(acc)
307+ case x :: xs => let(x) { (x : Term ) => rec(xs, x :: acc) }
308+ }
309+ rec(terms, Nil )
310+ }
286311}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ object Macros {
1010
1111 val rhsTerm = rhs.unseal
1212
13- import qctx .tasty .util . {let => letTerm }
13+ import qctx .tasty .{let => letTerm }
1414 letTerm(rhsTerm) { rhsId =>
1515 body(rhsId.seal.asInstanceOf [Expr [T ]]).unseal // Dangerous uncheked cast!
1616 }.seal.cast[Unit ]
You can’t perform that action at this time.
0 commit comments