Skip to content

Commit b26a067

Browse files
committed
Update reflection.md
1 parent 4c842b5 commit b26a067

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

docs/_docs/reference/metaprogramming/reflection.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ def macroImpl()(quotes: Quotes): Expr[Unit] =
9898
`quotes.reflect` contains three facilities for tree traversal and
9999
transformation.
100100

101-
`TreeAccumulator` ties the knot of a traversal. By calling `foldOver(x, tree)(owner)`
102-
we can dive into the `tree` node and start accumulating values of type `X` (e.g.,
103-
of type `List[Symbol]` if we want to collect symbols). The code below, for
104-
example, collects the `val` definitions in the tree.
101+
`TreeAccumulator[X]` allows you to traverse the tree and aggregate data of type `X` along the way, by overriding its method `foldTree(x: X, tree: Tree)(owner: Symbol): X`.
102+
103+
`foldOverTree(x: X, tree: Tree)(owner: Symbol): X` calls `foldTree` on each children of `tree` (using `fold` to give each call the value of the previous one).
104+
105+
The code below, for example, collects the `val` definitions in the tree.
105106

106107
```scala
107108
def collectPatternVariables(tree: Tree)(using ctx: Context): List[Symbol] =
@@ -115,12 +116,15 @@ def collectPatternVariables(tree: Tree)(using ctx: Context): List[Symbol] =
115116
acc(Nil, tree)
116117
```
117118

118-
A `TreeTraverser` extends a `TreeAccumulator` and performs the same traversal
119-
but without returning any value. Finally, a `TreeMap` performs a transformation.
119+
A `TreeTraverser` extends a `TreeAccumulator[Unit]` and performs the same traversal
120+
but without returning any value.
121+
122+
`TreeMap` transforms trees along the traversal, through overloading its methods it is possible to transform only trees of specific types, for example `transformStatement` only transforms `Statement`s.
123+
120124

121125
#### ValDef.let
122126

123-
`quotes.reflect.ValDef` also offers a method `let` that allows us to bind the `rhs` (right-hand side) to a `val` and use it in `body`.
127+
The object `quotes.reflect.ValDef` also offers a method `let` that allows us to bind the `rhs` (right-hand side) to a `val` and use it in `body`.
124128
Additionally, `lets` binds the given `terms` to names and allows to use them in the `body`.
125129
Their type definitions are shown below:
126130

0 commit comments

Comments
 (0)