You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/tasty-reflect.md
+18-14Lines changed: 18 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,19 +3,19 @@ layout: doc-page
3
3
title: "TASTy reflect"
4
4
---
5
5
6
-
TASTy reflect provides an API that allows inspection and construction of Typed Abstract Syntax Trees (TAST).
7
-
It may be used on quoted expressions (`quoted.Expr`) and types (`quoted.Type`) from [Principled Meta-programming](./principled-meta-programming.html)
6
+
TASTy Reflect enables inspection and construction of Typed Abstract Syntax Trees (TAST).
7
+
It may be used on quoted expressions (`quoted.Expr`) and quoted types (`quoted.Type`) from [Principled Meta-programming](./principled-meta-programming.html)
8
8
or on full TASTy files.
9
9
10
-
If you are starting using macros see first [Principled Meta-programming](./principled-meta-programming.html) and then follow with API (if really needed).
10
+
If you are writing macros, please first read [Principled Meta-programming](./principled-meta-programming.html).
11
+
You may find all you need without using TASTy Reflect.
11
12
12
13
13
14
## From quotes and splices to TASTs and back
14
15
15
-
`quoted.Expr` and `quoted.Type` are opaque TASTs.
16
-
The opaqueness required in [Principled Meta-programming](./principled-meta-programming.html) provide the guarantee that
17
-
the generation of code of the macro will be type correct.
18
-
Using TASTy reflect will break these guarantees and may fail at macro expansion time, hence additional explicit check must be done.
16
+
`quoted.Expr` and `quoted.Type` are only meant for generative meta-programming, generation of code without inspecting the ASTs.
17
+
[Principled Meta-programming](./principled-meta-programming.html) provides the guarantee that the generation of code will be type-correct.
18
+
Using TASTy Reflect will break these guarantees and may fail at macro expansion time, hence additional explicit check must be done.
19
19
20
20
21
21
To provide reflection capabilities in macro we need to add an implicit parameter of type `scala.tasty.Reflection` and import it in the scope where it is used.
`import reflection._` will provide a `reflect` extension method on `quoted.Expr` and `quoted.Type`with return a `reflection.Term` and `reflection.TypeTree` respectivly.
36
-
It will also import all extractors and methods on TASTy reflect trees. For example the `Term.Literal(_)` extractor used bellow.
35
+
`import reflection._` will provide a `reflect` extension method on `quoted.Expr` and `quoted.Type`which return a `reflection.Term` and `reflection.TypeTree` respectivly.
36
+
It will also import all extractors and methods on TASTy Reflect trees. For example the `Term.Literal(_)` extractor used bellow.
37
37
To easily know which extractor are needed the `reflection.Term.show` method returns the string representation of the extractors.
38
38
39
39
@@ -72,12 +72,16 @@ class Consumer extends TastyConsumer {
72
72
Then the consumer can be instantiated with the following code to get the tree of the class `foo.Bar` for a foo in the classpath.
0 commit comments