1
1
---
2
2
layout : doc-page
3
- title : " TASTy Reflect"
3
+ title : " Quoted Reflect"
4
4
---
5
5
6
- TASTy Reflect enables inspection and construction of Typed Abstract Syntax Trees
6
+ Reflection enables inspection and construction of Typed Abstract Syntax Trees
7
7
(Typed-AST). It may be used on quoted expressions (` quoted.Expr ` ) and quoted
8
8
types (` quoted.Type ` ) from [ Macros] ( ./macros.md ) or on full TASTy files.
9
9
10
10
If you are writing macros, please first read [ Macros] ( ./macros.md ) .
11
- You may find all you need without using TASTy Reflect .
11
+ You may find all you need without using reflection .
12
12
13
13
14
- ## API: From quotes and splices to TASTy reflect trees and back
14
+ ## API: From quotes and splices to reflect trees and back
15
15
16
16
With ` quoted.Expr ` and ` quoted.Type ` we can compute code but also analyze code
17
17
by inspecting the ASTs. [ Macros] ( ./macros.md ) provide the guarantee that the
18
- generation of code will be type-correct. Using TASTy Reflect will break these
18
+ generation of code will be type-correct. Using reflect will break these
19
19
guarantees and may fail at macro expansion time, hence additional explicit
20
20
checks must be done.
21
21
22
22
To provide reflection capabilities in macros we need to add an implicit
23
- parameter of type ` scala.quoted.QuoteContext ` and import ` tasty ._` from it in
23
+ parameter of type ` scala.quoted.QuoteContext ` and import ` reflect ._` from it in
24
24
the scope where it is used.
25
25
26
26
``` scala
@@ -29,19 +29,19 @@ import scala.quoted._
29
29
inline def natConst (x : => Int ): Int = $ {natConstImpl(' {x})}
30
30
31
31
def natConstImpl (x : Expr [Int ])(using qctx : QuoteContext ): Expr [Int ] = {
32
- import qctx .tasty ._
32
+ import qctx .reflect ._
33
33
...
34
34
}
35
35
```
36
36
37
37
### Extractors
38
38
39
- ` import qctx.tasty ._ ` will provide all extractors and methods on TASTy Reflect
39
+ ` import qctx.reflect ._ ` will provide all extractors and methods on reflect
40
40
trees. For example the ` Literal(_) ` extractor used below.
41
41
42
42
``` scala
43
43
def natConstImpl (x : Expr [Int ])(using qctx : QuoteContext ): Expr [Int ] = {
44
- import qctx .tasty ._
44
+ import qctx .reflect ._
45
45
val xTree : Term = x.unseal
46
46
xTree match {
47
47
case Inlined (_, _, Literal (Constant (n : Int ))) =>
@@ -59,9 +59,9 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
59
59
```
60
60
61
61
To easily know which extractors are needed, the ` showExtractors ` method on a
62
- ` qctx.tasty .Term ` returns the string representation of the extractors.
62
+ ` qctx.reflect .Term ` returns the string representation of the extractors.
63
63
64
- The method ` qctx.tasty .Term.seal ` provides a way to go back to a
64
+ The method ` qctx.reflect .Term.seal ` provides a way to go back to a
65
65
` quoted.Expr[Any] ` . Note that the type is ` Expr[Any] ` . Consequently, the type
66
66
must be set explicitly with a checked ` cast ` call. If the type does not conform
67
67
to it an exception will be thrown at runtime.
@@ -77,7 +77,7 @@ operation expression passed while calling the `macro` below.
77
77
inline def macro (param : => Boolean ): Unit = $ { macroImpl(' param ) }
78
78
79
79
def macroImpl (param : Expr [Boolean ])(using qctx : QuoteContext ): Expr [Unit ] = {
80
- import qctx .tasty ._
80
+ import qctx .reflect ._
81
81
import util ._
82
82
83
83
param.unseal.underlyingArgument match {
99
99
100
100
``` scala
101
101
def macroImpl ()(qctx : QuoteContext ): Expr [Unit ] = {
102
- import qctx .tasty ._
102
+ import qctx .reflect ._
103
103
val pos = rootPosition
104
104
105
105
val path = pos.sourceFile.jpath.toString
@@ -136,7 +136,7 @@ def collectPatternVariables(tree: Tree)(implicit ctx: Context): List[Symbol] = {
136
136
```
137
137
138
138
A `TreeTraverser` extends a `TreeAccumulator` and performs the same traversal
139
- but without returning any value. Finally a `TreeMap` performs a transformation.
139
+ but without returning any value. Finally , a `TreeMap` performs a transformation.
140
140
141
141
#### Let
142
142
@@ -150,8 +150,3 @@ def let(rhs: Term)(body: Ident => Term): Term = ...
150
150
151
151
def lets (terms : List [Term ])(body : List [Term ] => Term ): Term = ...
152
152
```
153
-
154
- ## More Examples
155
-
156
- * Start experimenting with TASTy Reflect ([link](https:// github.com/ nicolasstucki/ tasty- reflection- exercise))
157
-
0 commit comments