Skip to content

Commit b6f0aff

Browse files
committed
updated power example (metaprogramming)
1 parent ec15527 commit b6f0aff

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

docs/docs/reference/contextual/context-bounds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A context bound is a shorthand for expressing the common pattern of a context pa
99
def maximum[T: Ord](xs: List[T]): T = xs.reduceLeft(max)
1010
```
1111

12-
A bound like `: Ord` on a type parameter `T` of a method or class indicates a context parameter `with Ord[T]`. The context parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,
12+
A bound like `: Ord` on a type parameter `T` of a method or class indicates a context parameter `with Ord[T]`. The context parameter(s) generated from context bounds come last in the definition of the containing method or class. For instance,
1313

1414
```scala
1515
def f[T: C1 : C2, U: C3](x: T)(using y: U, z: V): R

docs/docs/reference/metaprogramming/macros-spec.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -166,26 +166,36 @@ in that it does not allow for the inspection of quoted expressions and
166166
types. It’s possible to work around this by providing all necessary
167167
information as normal, unquoted inline parameters. But we would gain
168168
more flexibility by allowing for the inspection of quoted code with
169-
pattern matching. This opens new possibilities. For instance, here is a
170-
version of `power` that generates the multiplications directly if the
171-
exponent is statically known and falls back to the dynamic
172-
implementation of power otherwise.
169+
pattern matching. This opens new possibilities.
170+
171+
For instance, here is a version of `power` that generates the multiplications
172+
directly if the exponent is statically known and falls back to the dynamic
173+
implementation of `power` otherwise.
173174
```scala
174-
inline def power(n: Int, x: Double): Double = ${
175-
'n match {
176-
case Constant(n1) => powerCode(n1, 'x)
177-
case _ => '{ dynamicPower(n, x) }
178-
}
179-
}
175+
import scala.quoted.Expr
176+
import scala.quoted.Quotes
180177

181-
private def dynamicPower(n: Int, x: Double): Double =
182-
if (n == 0) 1.0
183-
else if (n % 2 == 0) dynamicPower(n / 2, x * x)
184-
else x * dynamicPower(n - 1, x)
185-
```
178+
inline def power(x: Double, n: Int): Double =
179+
${ powerExpr('x, 'n) }
186180

187-
This assumes a `Constant` extractor that maps tree nodes representing
188-
constants to their values.
181+
private def powerExpr(x: Expr[Double], n: Expr[Int])
182+
(using Quotes): Expr[Double] =
183+
n.value match
184+
case Some(m) => powerExpr(x, m)
185+
case _ => '{ dynamicPower($x, $n) }
186+
187+
private def powerExpr(x: Expr[Double], n: Int)
188+
(using Quotes): Expr[Double] =
189+
if n == 0 then '{ 1.0 }
190+
else if n == 1 then x
191+
else if n % 2 == 0 then '{ val y = $x * $x; ${ powerExpr('y, n / 2) } }
192+
else '{ $x * ${ powerExpr(x, n - 1) } }
193+
194+
private def dynamicPower(x: Double, n: Int): Double =
195+
if n == 0 then 1.0
196+
else if n % 2 == 0 then dynamicPower(x * x, n / 2)
197+
else x * dynamicPower(x, n - 1)
198+
```
189199

190200
With the right extractors, the "AsFunction" conversion
191201
that maps expressions over functions to functions over expressions can
@@ -216,7 +226,7 @@ This would allow constructing applications from lists of arguments
216226
without having to match the arguments one-by-one with the
217227
corresponding formal parameter types of the function. We then need "at
218228
the end" a method to convert an `Expr[Any]` to an `Expr[T]` where `T` is
219-
given from the outside. E.g. if `code` yields a `Expr[Any]`, then
229+
given from the outside. For instance, if `code` yields a `Expr[Any]`, then
220230
`code.atType[T]` yields an `Expr[T]`. The `atType` method has to be
221231
implemented as a primitive; it would check that the computed type
222232
structure of `Expr` is a subtype of the type structure representing

0 commit comments

Comments
 (0)