Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ either a constant or is a parameter that will be a constant when instantiated. T
aspect is also important for macro expansion.

To get values out of expressions containing constants `Expr` provides the method
`getValue` (or `value`). This will convert the `Expr[T]` into a `Some[T]` (or `T`) when the
`unlift` (or `unliftOrError`). This will convert the `Expr[T]` into a `Some[T]` (or `T`) when the
expression contains value. Otherwise it will retrun `None` (or emit an error).
To avoid having incidental val bindings generated by the inlining of the `def`
it is recommended to use an inline parameter. To illustrate this, consider an
Expand All @@ -442,7 +442,7 @@ implementation of the `power` function that makes use of a statically known expo
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }

private def powerCode(x: Expr[Double], n: Expr[Int])(using QuoteContext): Expr[Double] =
n.getValue match
n.unlift match
case Some(m) => powerCode(x, m)
case None => '{ Math.pow($x, $y) }

Expand Down Expand Up @@ -604,7 +604,7 @@ inline method that can calculate either a value of type `Int` or a value of type
inline def defaultOf(inline str: String) <: Any = ${ defaultOfImpl('str) }

def defaultOfImpl(strExpr: Expr[String])(using QuoteContext): Expr[Any] =
strExpr.value match
strExpr.unliftOrError match
case "int" => '{1}
case "string" => '{"a"}

Expand Down Expand Up @@ -633,7 +633,7 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
`scala.quoted` contains objects that can help extracting values from `Expr`.

* `scala.quoted.Const`/`scala.quoted.Consts`: matches an expression of a literal value (or list of values) and returns the value (or list of values).
* `scala.quoted.Value`/`scala.quoted.Values`: matches an expression of a value (or list of values) and returns the value (or list of values).
* `scala.quoted.Unlifted`: matches an expression of a value (or list of values) and returns the value (or list of values).
* `scala.quoted.Varargs`: matches an explicit sequence of expresions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.

These could be used in the following way to optimize any call to `sum` that has statically known values.
Expand Down Expand Up @@ -671,7 +671,7 @@ optimize {

```scala
def sum(args: Int*): Int = args.sum
inline def optimize(arg: Int): Int = ${ optimizeExpr('arg) }
inline def optimize(inline arg: Int): Int = ${ optimizeExpr('arg) }
private def optimizeExpr(body: Expr[Int])(using QuoteContext): Expr[Int] = body match {
// Match a call to sum without any arguments
case '{ sum() } => Expr(0)
Expand Down Expand Up @@ -717,7 +717,7 @@ This might be used to then perform an implicit search as in:


```scala
inline def (sc: StringContext).showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }
inline def (inline sc: StringContext).showMe(inline args: Any*): String = ${ showMeExpr('sc, 'args) }

private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] = {
argsExpr match {
Expand Down