Skip to content

Commit 0f4d3b2

Browse files
Merge pull request #7039 from dotty-staging/fix-tasty-reflect-doc
Adjust example and fix showExtractors note on tasty-reflect doc
2 parents 0ebbcff + aa68234 commit 0f4d3b2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ guarantees and may fail at macro expansion time, hence additional explicit
2020
checks must be done.
2121

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

2626
```scala
2727
import scala.quoted._
@@ -47,18 +47,22 @@ def natConstImpl(x: Expr[Int]) given (qctx: QuoteContext): Expr[Int] = {
4747
import qctx.tasty._
4848
val xTree: Term = x.unseal
4949
xTree match {
50-
case Term.Literal(Constant.Int(n)) =>
51-
if (n <= 0)
52-
QuoteError("Parameter must be natural number")
53-
n.toExpr
50+
case Inlined(_, _, Literal(Constant(n: Int))) =>
51+
if (n <= 0) {
52+
qctx.error("Parameter must be natural number")
53+
'{0}
54+
} else {
55+
xTree.seal.cast[Int]
56+
}
5457
case _ =>
55-
QuoteError("Parameter must be a known constant")
58+
qctx.error("Parameter must be a known constant")
59+
'{0}
5660
}
5761
}
5862
```
5963

60-
To easily know which extractors are needed, the `qctx.tasty.Term.show` method
61-
returns the string representation of the extractors.
64+
To easily know which extractors are needed, the `showExtractors` method on a
65+
`qctx.tasty.Term` returns the string representation of the extractors.
6266

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

0 commit comments

Comments
 (0)