Skip to content

Update Scala 3 macro reflection #1895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _overviews/scala3-macros/tutorial/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ abstractions and offers more fine-grained control.

- [Compile-time operations][compiletime] offer additional metaprogramming utilities that can be used within `inline` methods (for example to improve error reporting), without having to define a macro.

- Starting from `inline`-methods, [macros][macros] are programs that explicitly operate on programs.
- Starting from `inline` methods, [macros][macros] are programs that explicitly operate on programs.

- Macros can be defined in terms of a _high-level_ API of [quoted expressions][quotes], that admits simple construction and deconstruction of programs expressions.

- Macros can also be defined in terms of a more _low-level_ API of [TASTy Reflection][tasty], that allows detailed inspection of programs.
- Macros can also be defined in terms of a more _low-level_ API of [Reflection][reflection], that allows detailed inspection of programs.

> The tutorial uses the API of Scala 3.0.0-M3. The API had many small changes in this revision.

> 🚧 We are still in the process of writing the tutorial. You can [help us improve it][contributing] 🚧
> 🚧 We are still in the process of writing the tutorial. You can [help us][contributing] 🚧

[contributing]: {% link scala3/contribute-to-docs.md %}
[compiletime]: {% link _overviews/scala3-macros/tutorial/compiletime.md %}
[inline]: {% link _overviews/scala3-macros/tutorial/inline.md %}
[macros]: {% link _overviews/scala3-macros/tutorial/macros.md %}
[quotes]: {% link _overviews/scala3-macros/tutorial/quotes.md %}
[tasty]: {% link _overviews/scala3-macros/tutorial/reflection.md %}
[reflection]: {% link _overviews/scala3-macros/tutorial/reflection.md %}
14 changes: 7 additions & 7 deletions _overviews/scala3-macros/tutorial/reflection.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
type: section
title: Tasty Reflection
title: Reflection
num: 6

previous-page: quotes
---

The reflection API provides a more complex and comprehensive view on the structure of the code.
It provides a view on the *Typed Abstract Syntax Trees* **TASTy** and their properties such as types, symbols, positions and comments.
It provides a view on the *Typed Abstract Syntax Trees* and their properties such as types, symbols, positions and comments.

## How to use the API

Expand All @@ -16,19 +16,19 @@ We can use `scala.quoted.quotes` to import it.

```scala
def pow(x: Expr[Int])(using Quotes): Expr[Int] = {
import quotes.tasty._ // Import Tree, Type, Symbol, Position, .....
import quotes.reflect._ // Import Tree, Type, Symbol, Position, .....
...
}
```

This will import all the types and modules (with extension methods) of the API.

The full imported API can be found here: [Reflection](https://dotty.epfl.ch/api/scala/tasty/Reflection.html)
The full imported API can be found here: [Reflection](https://dotty.epfl.ch/api/scala/quoted/Quotes$reflectModule.html?query=trait%20reflectModule)

For example to find what is a `Term`, we can see in the hierarchy that it is a subtype of `Statement` which is a subtype of `Tree`.
If we look into the [`TermMethods`](https://dotty.epfl.ch/api/scala/tasty/Reflection/TermMethods.html) we will find all the extension methods that are defined for `Term` such as `Term.tpe` which returns a `Type`.
As it is a subtype of `Tree` we can also look into the [`TreeMethods`](http://dotty.epfl.ch/api/scala/tasty/Reflection/TreeMethods.html) to find more methods such as `Tree.pos`.
Each type also a module with some _static-ish_ methods, for example in the [TypeModule](http://dotty.epfl.ch/api/scala/tasty/Reflection/TypeModule.html) we can find the method `Type.of[T]` with will create an instance of `Type` containing `T`.
If we look into the [`TermMethods`](https://dotty.epfl.ch/api/scala/quoted/Quotes$reflectModule$TermMethods.html) we will find all the extension methods that are defined for `Term` such as `Term.tpe` which returns a `Type`.
As it is a subtype of `Tree` we can also look into the [`TreeMethods`](https://dotty.epfl.ch/api/scala/quoted/Quotes$reflectModule$TreeMethods.html) to find more methods such as `Tree.pos`.
Each type also a module with some _static-ish_ methods, for example in the [`TypeReprModule`](https://dotty.epfl.ch/api/scala/quoted/Quotes$reflectModule$TypeReprModule.html) we can find the method `TypeRepr.of[T]` with will create an instance of `Type` containing `T`.


## Relation with expressions
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ bundle exec htmlproofer ./_site/\
--http-status-ignore "400,401,429"\
--empty-alt-ignore\
--allow-hash-href\
--url-ignore '/https://github.com/scala/docs.scala-lang/blob/master/.*/,/www.oracle.com/,/.*dotty.epfl.ch\/api\/scala\/tasty\/Reflection.*/'
--url-ignore '/https://github.com/scala/docs.scala-lang/blob/master/.*/,/www.oracle.com/'

exit 0