Skip to content

Commit f377ddb

Browse files
committed
Update metaprogramming docs
1 parent a42fe92 commit f377ddb

File tree

7 files changed

+35
-42
lines changed

7 files changed

+35
-42
lines changed

docs/blog/_posts/2019-01-21-12th-dotty-milestone-release.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ We also connect the new lower-level reflection layer to the existing principled
9292
- `unseal` that unseals an `Expr[T]` (non traversable code) into a `Term` and
9393
- `seal` that seals back a `Term` into an `Expr[T]`.
9494

95-
Read the [relevant documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/tasty-reflect.html) to learn how to go from quotes and splices to TASTys Reflect trees and back .
95+
Read the [relevant documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/quote-reflection.html) to learn how to go from quotes and splices to TASTys Reflect trees and back .
9696

9797
### Alignments with the Scala Improvement Process
9898

docs/docs/reference/dropped-features/macros.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The previous, experimental macro system has been dropped.
88
Instead, there is a cleaner, more restricted system based on two complementary concepts: `inline` and `'{ ... }`/`${ ... }` code generation.
99
`'{ ... }` delays the compilation of the code and produces an object containing the code, dually `${ ... }` evaluates an expression which produces code and inserts it in the surrounding `${ ... }`.
1010
In this setting, a definition marked as inlined containing a `${ ... }` is a macro, the code inside the `${ ... }` is executed at compile-time and produces code in the form of `'{ ... }`.
11-
Additionally, the contents of code can be inspected and created with a more complex reflection API (TASTy Reflect) as an extension of `'{ ... }`/`${ ... }` framework.
11+
Additionally, the contents of code can be inspected and created with a more complex reflection API as an extension of `'{ ... }`/`${ ... }` framework.
1212

1313
* `inline` has been [implemented](../metaprogramming/inline.md) in Scala 3.
1414
* Quotes `'{ ... }` and splices `${ ... }` has been [implemented](../metaprogramming/macros.md) in Scala 3.
15-
* [TASTy reflect](../metaprogramming/tasty-reflect.md) provides more complex tree based APIs to inspect or create quoted code.
15+
* [TASTy reflect](../metaprogramming/quote-reflection.md) provides more complex tree based APIs to inspect or create quoted code.

docs/docs/reference/metaprogramming/tasty-reflect.md renamed to docs/docs/reference/metaprogramming/quote-reflection.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
22
layout: doc-page
3-
title: "TASTy Reflect"
3+
title: "Quote Reflection"
44
---
55

6-
TASTy Reflect enables inspection and construction of Typed Abstract Syntax Trees
6+
Quote Reflection enables inspection and construction of Typed Abstract Syntax Trees
77
(Typed-AST). It may be used on quoted expressions (`quoted.Expr`) and quoted
88
types (`quoted.Type`) from [Macros](./macros.md) or on full TASTy files.
99

1010
If you are writing macros, please first read [Macros](./macros.md).
11-
You may find all you need without using TASTy Reflect.
11+
You may find all you need without using quote reflection.
1212

1313

14-
## API: From quotes and splices to TASTy reflect trees and back
14+
## API: From quotes and splices to reflect trees and back
1515

1616
With `quoted.Expr` and `quoted.Type` we can compute code but also analyze code
1717
by inspecting the ASTs. [Macros](./macros.md) provide the guarantee that the
18-
generation of code will be type-correct. Using TASTy Reflect will break these
18+
generation of code will be type-correct. Using quote reflection will break these
1919
guarantees and may fail at macro expansion time, hence additional explicit
2020
checks must be done.
2121

@@ -35,20 +35,20 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
3535

3636
### Extractors
3737

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

4141
```scala
4242
def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
4343
import quotes.reflect._
44-
val xTree: Term = x.asTerm
45-
xTree match
44+
val tree: Term = x.asTerm
45+
tree match
4646
case Inlined(_, _, Literal(IntConstant(n))) =>
4747
if n <= 0 then
4848
report.error("Parameter must be natural number")
4949
'{0}
5050
else
51-
xTree.asExprOf[Int]
51+
tree.asExprOf[Int]
5252
case _ =>
5353
report.error("Parameter must be a known constant")
5454
'{0}
@@ -57,9 +57,9 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
5757
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.
5858

5959
```scala
60-
xTree.show(using Printer.TreeStructure)
60+
tree.show(using Printer.TreeStructure)
6161
// or
62-
Printer.TreeStructure.show(xTree)
62+
Printer.TreeStructure.show(tree)
6363
```
6464

6565

@@ -94,7 +94,7 @@ def macroImpl()(quotes: Quotes): Expr[Unit] =
9494

9595
### Tree Utilities
9696

97-
`scala.tasty.reflect` contains three facilities for tree traversal and
97+
`quotes.reflect` contains three facilities for tree traversal and
9898
transformation.
9999

100100
`TreeAccumulator` ties the knot of a traversal. By calling `foldOver(x, tree))`
@@ -114,9 +114,9 @@ def collectPatternVariables(tree: Tree)(implicit ctx: Context): List[Symbol] =
114114
A `TreeTraverser` extends a `TreeAccumulator` and performs the same traversal
115115
but without returning any value. Finally a `TreeMap` performs a transformation.
116116

117-
#### Let
117+
#### ValDef.let
118118

119-
`scala.tasty.Reflection` also offers a method `let` that allows us
119+
`quotes.reflect.ValDef` also offers a method `let` that allows us
120120
to bind the `rhs` (right-hand side) to a `val` and use it in `body`. Additionally, `lets` binds
121121
the given `terms` to names and allows to use them in the `body`. Their type definitions
122122
are shown below:
@@ -126,7 +126,3 @@ def let(rhs: Term)(body: Ident => Term): Term = ...
126126

127127
def lets(terms: List[Term])(body: List[Term] => Term): Term = ...
128128
```
129-
130-
## More Examples
131-
132-
* Start experimenting with TASTy Reflect ([link](https://github.com/nicolasstucki/tasty-reflection-exercise))

docs/docs/reference/metaprogramming/staging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: doc-page
3-
title: "Multi-Stage Programming"
3+
title: "Runtime Multi-Stage Programming"
44
---
55

66
The framework expresses at the same time compile-time metaprogramming and

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,27 @@ through the TASTy reflect API.
1616

1717
## Inspecting TASTy files
1818

19-
To inspect the TASTy Reflect trees of a TASTy file a consumer can be defined in
20-
the following way.
19+
To inspect the trees of a TASTy file a consumer can be defined in the following way.
2120

2221
```scala
23-
import scala.tasty.Reflection
24-
import scala.tasty.file._
22+
import scala.quoted._
23+
import scala.tasty.inspector._
2524

26-
class Consumer extends TastyInspector:
27-
final def apply(reflect: Reflection)(root: reflect.Tree): Unit =
28-
import reflect._
29-
// Do something with the tree
25+
class MyInspector extends TastyInspector:
26+
protected def processCompilationUnit(using Quotes)(tree: quotes.reflect.Tree): Unit =
27+
import quotes.reflect._
28+
// Do something with the tree
3029
```
3130

32-
Then the consumer can be instantiated with the following code to get the tree of
33-
the class `foo.Bar` for a foo in the classpath.
31+
Then the consumer can be instantiated with the following code to get the tree of the `foo/Bar.tasty` file.
3432

3533
```scala
3634
object Test:
37-
def main(args: Array[String]): Unit =
38-
InspectTasty("", List("foo.Bar"), new Consumer)
35+
def main(args: Array[String]): Unit =
36+
new MyInspector().inspectTastyFiles("foo/Bar.tasty")
3937
```
4038

41-
Note that if we need to run the main (in the example below defined in an object called `Test`) after
42-
compilation we need to make the compiler available to the runtime:
39+
Note that if we need to run the main (in the example below defined in an object called `Test`) after compilation we need to make the compiler available to the runtime:
4340

4441
```shell
4542
scalac -d out Test.scala

docs/docs/reference/metaprogramming/toc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ introduce the following fundamental facilities:
2525
to program code. Together with `inline`, these two abstractions allow
2626
to construct program code programmatically.
2727

28-
3. [Staging](./staging.md) Where macros construct code at _compile-time_,
28+
3. [Runtime Staging](./staging.md) Where macros construct code at _compile-time_,
2929
staging lets programs construct new code at _runtime_. That way,
3030
code generation can depend not only on static data but also on data available at runtime. This splits the evaluation of the program in two or more phases or ...
3131
stages. Consequently, this method of generative programming is called "Multi-Stage Programming". Staging is built on the same foundations as macros. It uses
3232
quotes and splices, but leaves out `inline`.
3333

34-
4. [TASTy Reflection](./tasty-reflect.md) Quotations are a "black-box"
34+
4. [Quote Reflection](./quote-reflection.md) Quotations are a "black-box"
3535
representation of code. They can be parameterized and composed using
3636
splices, but their structure cannot be analyzed from the outside. TASTy
3737
reflection gives a way to analyze code structure by partly revealing the representation type of a piece of code in a standard API. The representation

docs/sidebar.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ sidebar:
8181
url: docs/reference/metaprogramming/inline.html
8282
- title: Macros
8383
url: docs/reference/metaprogramming/macros.html
84-
- title: Staging
84+
- title: Runtime Staging
8585
url: docs/reference/metaprogramming/staging.html
86-
- title: TASTy Reflection
87-
url: docs/reference/metaprogramming/tasty-reflect.html
86+
- title: QuoteReflection
87+
url: docs/reference/metaprogramming/quote-reflection.html
8888
- title: TASTy Inspection
8989
url: docs/reference/metaprogramming/tasty-inspect.html
9090
- title: Other New Features

0 commit comments

Comments
 (0)