Skip to content
Merged
Changes from all commits
Commits
File filter

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