Skip to content

Commit c759692

Browse files
committed
Fix example code in macros.md
- Fix some function definitions by adding `(using QuoteContext)` - Add a comment in the body of the `assertImpl` definition - `showMeExpr` doesn't need a named QuoteContext, and use the anonymous form - Adjust the order of the parameter lists of `evalExpr`
1 parent e67908d commit c759692

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ object Macros {
375375
inline def assert(inline expr: Boolean): Unit =
376376
${ assertImpl('expr) }
377377

378-
def assertImpl(expr: Expr[Boolean]) =
379-
'{ if !($expr) then throw new AssertionError("failed assertion: " + ${expr.show}) }
378+
def assertImpl(expr: Expr[Boolean])(using QuoteContext) =
379+
'{ if !($expr) then throw new AssertionError("failed assertion: " + ${expr.show}) } // autolift is applied
380380
}
381381

382382
object App {
@@ -502,7 +502,7 @@ function `f` and one `sum` that performs a sum by delegating to `map`.
502502

503503
```scala
504504
object Macros {
505-
def map[T](arr: Expr[Array[T]], f: Expr[T] => Expr[Unit])(implicit t: Type[T]): Expr[Unit] = '{
505+
def map[T](arr: Expr[Array[T]], f: Expr[T] => Expr[Unit])(using t: Type[T], qctx: QuoteContext): Expr[Unit] = '{
506506
var i: Int = 0
507507
while (i < ($arr).length) {
508508
val element: $t = ($arr)(i)
@@ -511,7 +511,7 @@ object Macros {
511511
}
512512
}
513513

514-
def sum(arr: Expr[Array[Int]]): Expr[Int] = '{
514+
def sum(arr: Expr[Array[Int]])(using QuoteContext): Expr[Int] = '{
515515
var sum = 0
516516
${ map(arr, x => '{sum += $x}) }
517517
sum
@@ -719,7 +719,7 @@ This might be used to then perform an implicit search as in:
719719
```scala
720720
inline def (inline sc: StringContext).showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }
721721

722-
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] = {
722+
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using QuoteContext): Expr[String] = {
723723
argsExpr match {
724724
case Varargs(argExprs) =>
725725
val argShowedExprs = argExprs.map {
@@ -765,7 +765,7 @@ the subxpression of type `Expr[Int]` is bound to `body` as an `Expr[Int => Int]`
765765
```scala
766766
inline def eval(inline e: Int): Int = ${ evalExpr('e) }
767767

768-
private def evalExpr(using QuoteContext)(e: Expr[Int]): Expr[Int] = {
768+
private def evalExpr(e: Expr[Int])(using QuoteContext): Expr[Int] = {
769769
e match {
770770
case '{ val y: Int = $x; $body(y): Int } =>
771771
// body: Expr[Int => Int] where the argument represents references to y

0 commit comments

Comments
 (0)