Skip to content

Commit 64a089f

Browse files
Merge pull request #10932 from ShapelessCat/fix-docs_TASTy-Reflect
Fix a code example in tasty-reflect.md
2 parents a42fe92 + d57b477 commit 64a089f

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ information from the code. To avoid the hassle of working directly with the TAST
1313
file we provide the `TastyInspector` which loads the contents and exposes it
1414
through the TASTy reflect API.
1515

16-
1716
## Inspecting TASTy files
1817

1918
To inspect the TASTy Reflect trees of a TASTy file a consumer can be defined in
@@ -46,10 +45,12 @@ scalac -d out Test.scala
4645
scala -with-compiler -classpath out Test
4746
```
4847

49-
5048
## Template project
49+
5150
Using sbt version `1.1.5+`, do:
52-
```
51+
52+
```shell
5353
sbt new scala/scala3-tasty-inspector.g8
5454
```
55+
5556
in the folder where you want to clone the template.

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

Lines changed: 27 additions & 26 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
@@ -19,9 +18,9 @@ generation of code will be type-correct. Using TASTy Reflect will break these
1918
guarantees and may fail at macro expansion time, hence additional explicit
2019
checks must be done.
2120

22-
To provide reflection capabilities in macros we need to add an implicit
23-
parameter of type `scala.quoted.Quotes` and import `quotes.reflect._` from it in
24-
the scope where it is used.
21+
To provide reflection capabilities in macros we need to add an implicit parameter
22+
of type `scala.quoted.Quotes` and import `quotes.reflect._` from it in the scope
23+
where it is used.
2524

2625
```scala
2726
import scala.quoted._
@@ -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
@@ -99,15 +99,15 @@ transformation.
9999

100100
`TreeAccumulator` ties the knot of a traversal. By calling `foldOver(x, tree))`
101101
we can dive into the `tree` node and start accumulating values of type `X` (e.g.,
102-
of type List[Symbol] if we want to collect symbols). The code below, for
102+
of type `List[Symbol]` if we want to collect symbols). The code below, for
103103
example, collects the pattern variables of a tree.
104104

105105
```scala
106-
def collectPatternVariables(tree: Tree)(implicit ctx: Context): List[Symbol] =
106+
def collectPatternVariables(tree: Tree)(using ctx: Context): List[Symbol] =
107107
val acc = new TreeAccumulator[List[Symbol]]:
108-
def apply(syms: List[Symbol], tree: Tree)(implicit ctx: Context) = tree match
108+
def apply(syms: List[Symbol], tree: Tree)(using ctx: Context): List[Symbol] = 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)

docs/docs/reference/other-new-features/indentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ at the top-level, inside braces `{...}`, but not inside parentheses `(...)`, pat
105105

106106
### Optional Braces Around Template Bodies
107107

108-
The Scala grammar uses the term _template body_ for the definitions of a class, trait, or object that are normally enclosed in braces. The braces around a template body can also be omitted by means of the following rule
108+
The Scala grammar uses the term _template body_ for the definitions of a class, trait, or object that are normally enclosed in braces. The braces around a template body can also be omitted by means of the following rule.
109109

110110
If at the point where a template body can start there is a `:` that occurs at the end
111111
of a line, and that is followed by at least one indented statement, the recognized

0 commit comments

Comments
 (0)