@@ -20,7 +20,7 @@ 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.tasty.Reflection ` and import it in the scope where it
23
+ parameter of type ` scala.quoted.QuoteContext ` and import ` tasty._ ` from it in the scope where it
24
24
is used.
25
25
26
26
``` scala
@@ -29,23 +29,23 @@ import scala.tasty._
29
29
30
30
inline def natConst (x : => Int ): Int = $ {natConstImpl(' {x})}
31
31
32
- def natConstImpl (x : Expr [Int ])( implicit reflection : Reflection ): Expr [Int ] = {
33
- import reflection ._
32
+ def natConstImpl (x : Expr [Int ]) given ( qctx : QuoteContext ): Expr [Int ] = {
33
+ import qctx . tasty ._
34
34
...
35
35
}
36
36
```
37
37
38
38
### Sealing and Unsealing
39
39
40
- ` import reflection ._ ` will provide an ` unseal ` extension method on ` quoted.Expr `
41
- and ` quoted.Type ` which returns a ` reflection .Term` that represents the tree of
42
- the expression and ` reflection .TypeTree` that represents the tree of the type
40
+ ` import qctx.tasty ._ ` will provide an ` unseal ` extension method on ` quoted.Expr `
41
+ and ` quoted.Type ` which returns a ` qctx.tasty .Term` that represents the tree of
42
+ the expression and ` qctx.tasty .TypeTree` that represents the tree of the type
43
43
respectively. It will also import all extractors and methods on TASTy Reflect
44
44
trees. For example the ` Literal(_) ` extractor used below.
45
45
46
46
``` scala
47
- def natConstImpl (x : Expr [Int ])( implicit reflection : Reflection ): Expr [Int ] = {
48
- import reflection ._
47
+ def natConstImpl (x : Expr [Int ]) given ( qctx : QuoteContext ): Expr [Int ] = {
48
+ import qctx . tasty ._
49
49
val xTree : Term = x.unseal
50
50
xTree match {
51
51
case Term .Literal (Constant .Int (n)) =>
@@ -58,10 +58,10 @@ def natConstImpl(x: Expr[Int])(implicit reflection: Reflection): Expr[Int] = {
58
58
}
59
59
```
60
60
61
- To easily know which extractors are needed, the ` reflection .Term.show` method
61
+ To easily know which extractors are needed, the ` qctx.tasty .Term.show` method
62
62
returns the string representation of the extractors.
63
63
64
- The method ` reflection .Term.seal[T]` provides a way to go back to a
64
+ The method ` qctx.tasty .Term.seal[T]` 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. In the code above, we could have replaced
@@ -77,8 +77,8 @@ operation expression passed while calling the `macro` below.
77
77
``` scala
78
78
inline def macro (param : => Boolean ): Unit = $ { macroImpl(' param ) }
79
79
80
- def macroImpl (param : Expr [Boolean ])( implicit refl : Reflection ): Expr [Unit ] = {
81
- import refl ._
80
+ def macroImpl (param : Expr [Boolean ]) given ( qctx : QuoteContext ): Expr [Unit ] = {
81
+ import qctx . tasty ._
82
82
import util ._
83
83
84
84
param.unseal.underlyingArgument match {
@@ -99,8 +99,8 @@ such as the start line, the end line or even the source code at the expansion
99
99
point.
100
100
101
101
``` scala
102
- def macroImpl ()(reflect : Reflection ): Expr [Unit ] = {
103
- import reflect .{ Position => _ , _ }
102
+ def macroImpl ()(qctx : QuoteContext ): Expr [Unit ] = {
103
+ import qctx . tasty . _
104
104
val pos = rootPosition
105
105
106
106
val path = pos.sourceFile.jpath.toString
0 commit comments