Skip to content

Adjust example and fix showExtractors note on tasty-reflect doc #7039

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 1 commit into from
Aug 14, 2019
Merged
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
22 changes: 13 additions & 9 deletions docs/docs/reference/metaprogramming/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ guarantees and may fail at macro expansion time, hence additional explicit
checks must be done.

To provide reflection capabilities in macros we need to add an implicit
parameter of type `scala.quoted.QuoteContext` and import `tasty._` from it in the scope where it
is used.
parameter of type `scala.quoted.QuoteContext` and import `tasty._` from it in
the scope where it is used.

```scala
import scala.quoted._
Expand All @@ -47,18 +47,22 @@ def natConstImpl(x: Expr[Int]) given (qctx: QuoteContext): Expr[Int] = {
import qctx.tasty._
val xTree: Term = x.unseal
xTree match {
case Term.Literal(Constant.Int(n)) =>
if (n <= 0)
QuoteError("Parameter must be natural number")
n.toExpr
case Inlined(_, _, Literal(Constant(n: Int))) =>
if (n <= 0) {
qctx.error("Parameter must be natural number")
'{0}
} else {
xTree.seal.cast[Int]
}
case _ =>
QuoteError("Parameter must be a known constant")
qctx.error("Parameter must be a known constant")
'{0}
}
}
```

To easily know which extractors are needed, the `qctx.tasty.Term.show` method
returns the string representation of the extractors.
To easily know which extractors are needed, the `showExtractors` method on a
`qctx.tasty.Term` returns the string representation of the extractors.

The method `qctx.tasty.Term.seal[T]` provides a way to go back to a
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
Expand Down