File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed
docs/docs/reference/metaprogramming Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,8 @@ guarantees and may fail at macro expansion time, hence additional explicit
20
20
checks must be done.
21
21
22
22
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.
25
25
26
26
``` scala
27
27
import scala .quoted ._
@@ -47,18 +47,22 @@ def natConstImpl(x: Expr[Int]) given (qctx: QuoteContext): Expr[Int] = {
47
47
import qctx .tasty ._
48
48
val xTree : Term = x.unseal
49
49
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
+ }
54
57
case _ =>
55
- QuoteError (" Parameter must be a known constant" )
58
+ qctx.error(" Parameter must be a known constant" )
59
+ ' {0 }
56
60
}
57
61
}
58
62
```
59
63
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.
62
66
63
67
The method ` qctx.tasty.Term.seal[T] ` provides a way to go back to a
64
68
` quoted.Expr[Any] ` . Note that the type is ` Expr[Any] ` . Consequently, the type
You can’t perform that action at this time.
0 commit comments