Skip to content

Commit afef884

Browse files
committed
Fix a code example in tasty-reflect.md
1 parent 8fab91c commit afef884

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ types (`quoted.Type`) from [Macros](./macros.md) or on full TASTy files.
1010
If you are writing macros, please first read [Macros](./macros.md).
1111
You may find all you need without using TASTy Reflect.
1212

13-
1413
## API: From quotes and splices to TASTy reflect trees and back
1514

1615
With `quoted.Expr` and `quoted.Type` we can compute code but also analyze code
@@ -35,8 +34,8 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
3534

3635
### Extractors
3736

38-
`import quotes.reflect._` will provide all extractors and methods on TASTy Reflect
39-
trees. For example the `Literal(_)` extractor used below.
37+
`import quotes.reflect._` will provide all extractors and methods on TASTy
38+
Reflect trees. For example the `Literal(_)` extractor used below.
4039

4140
```scala
4241
def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
@@ -54,32 +53,33 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
5453
'{0}
5554
```
5655

57-
We can easily know which extractors are needed using `Printer.TreeStructure.show`, which returns the string representation the structure of the tree. Other printers can also be found in the `Printer` module.
56+
We can easily know which extractors are needed using `Printer.TreeStructure.show`,
57+
which returns the string representation the structure of the tree. Other printers
58+
can also be found in the `Printer` module.
5859

5960
```scala
6061
xTree.show(using Printer.TreeStructure)
6162
// or
6263
Printer.TreeStructure.show(xTree)
6364
```
6465

65-
66-
The methods `quotes.reflect.Term.{asExpr, asExprOf}` provide a way to go back to a `quoted.Expr`.
67-
Note that `asExpr` returns a `Expr[Any]`.
68-
On the other hand `asExprOf[T]` returns a `Expr[T]`, if the type does not conform to it an exception will be thrown at runtime.
69-
66+
The methods `quotes.reflect.Term.{asExpr, asExprOf}` provide a way to go back to
67+
a `quoted.Expr`. Note that `asExpr` returns a `Expr[Any]`. On the other hand
68+
`asExprOf[T]` returns a `Expr[T]`, if the type does not conform to it an exception
69+
will be thrown at runtime.
7070

7171
### Positions
7272

73-
The `ast` in the context provides a `rootPosition` value. It corresponds to
74-
the expansion site for macros. The macro authors can obtain various information about that
75-
expansion site. The example below shows how we can obtain position information
76-
such as the start line, the end line or even the source code at the expansion
77-
point.
73+
The `Position` in the context provides an `ofMacroExpansion` value. It corresponds
74+
to the expansion site for macros. The macro authors can obtain various information
75+
about that expansion site. The example below shows how we can obtain position
76+
information such as the start line, the end line or even the source code at the
77+
expansion point.
7878

7979
```scala
8080
def macroImpl()(quotes: Quotes): Expr[Unit] =
8181
import quotes.reflect._
82-
val pos = rootPosition
82+
val pos = Position.ofMacroExpansion
8383

8484
val path = pos.sourceFile.jpath.toString
8585
val start = pos.start
@@ -107,7 +107,7 @@ def collectPatternVariables(tree: Tree)(implicit ctx: Context): List[Symbol] =
107107
val acc = new TreeAccumulator[List[Symbol]]:
108108
def apply(syms: List[Symbol], tree: Tree)(implicit ctx: Context) = tree match
109109
case Bind(_, body) => apply(tree.symbol :: syms, body)
110-
case _ => foldOver(syms, tree)
110+
case _ => foldOver(syms, tree)
111111
acc(Nil, tree)
112112
```
113113

@@ -116,10 +116,10 @@ but without returning any value. Finally a `TreeMap` performs a transformation.
116116

117117
#### Let
118118

119-
`scala.tasty.Reflection` also offers a method `let` that allows us
120-
to bind the `rhs` (right-hand side) to a `val` and use it in `body`. Additionally, `lets` binds
121-
the given `terms` to names and allows to use them in the `body`. Their type definitions
122-
are shown below:
119+
`scala.tasty.Reflection` also offers a method `let` that allows us to bind the
120+
`rhs` (right-hand side) to a `val` and use it in `body`. Additionally, `lets`
121+
binds the given `terms` to names and allows to use them in the `body`. Their type
122+
definitions are shown below:
123123

124124
```scala
125125
def let(rhs: Term)(body: Ident => Term): Term = ...
@@ -130,3 +130,4 @@ def lets(terms: List[Term])(body: List[Term] => Term): Term = ...
130130
## More Examples
131131

132132
* Start experimenting with TASTy Reflect ([link](https://github.com/nicolasstucki/tasty-reflection-exercise))
133+
(outdated, need update)

0 commit comments

Comments
 (0)