Skip to content

Commit ff0bdd8

Browse files
committed
Fix a code example sumExpr in macros.md
1 parent dd462eb commit ff0bdd8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,12 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
632632

633633
`scala.quoted` contains objects that can help extracting values from `Expr`.
634634

635-
* `scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
636-
* `scala.quoted.Const`/`scala.quoted.Consts`: Same as `Expr`/`Exprs` but only works on primitive values.
637-
* `scala.quoted.Varargs`: matches an explicit sequence of expressions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
638-
635+
- `scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
636+
- `scala.quoted.Const`/`scala.quoted.Consts`: Same as `Expr`/`Exprs` but only works on primitive values.
637+
- `scala.quoted.Varargs`: matches an explicit sequence of expressions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
639638

640639
These could be used in the following way to optimize any call to `sum` that has statically known values.
640+
641641
```scala
642642
inline def sum(inline args: Int*): Int = ${ sumExpr('args) }
643643
private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match {
@@ -646,7 +646,7 @@ private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExp
646646
// argValues is of type Seq[Int]
647647
Expr(argValues.sum) // precompute result of sum
648648
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
649-
val staticSum: Int = argExprs.map(_.value.getOrElse(0))
649+
val staticSum: Int = argExprs.map(_.value.getOrElse(0)).sum
650650
val dynamicSum: Seq[Expr[Int]] = argExprs.filter(_.value.isEmpty)
651651
dynamicSum.foldLeft(Expr(staticSum))((acc, arg) => '{ $acc + $arg })
652652
case _ =>

0 commit comments

Comments
 (0)