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)
40
40
def typeChecks (code : String )(implicit ctx : Context ): Boolean = kernel.typeChecks(code)(ctx)
41
41
}
42
42
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
-
48
43
}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ trait TreeUtils
6
6
extends Core
7
7
with PatternOps
8
8
with SymbolOps
9
- with TreeOps {
9
+ with TreeOps { self : Reflection =>
10
10
11
11
abstract class TreeAccumulator [X ] {
12
12
@@ -283,4 +283,29 @@ trait TreeUtils
283
283
284
284
}
285
285
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
+ }
286
311
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ object Macros {
10
10
11
11
val rhsTerm = rhs.unseal
12
12
13
- import qctx .tasty .util . {let => letTerm }
13
+ import qctx .tasty .{let => letTerm }
14
14
letTerm(rhsTerm) { rhsId =>
15
15
body(rhsId.seal.asInstanceOf [Expr [T ]]).unseal // Dangerous uncheked cast!
16
16
}.seal.cast[Unit ]
You can’t perform that action at this time.
0 commit comments