Skip to content

Commit 04625bd

Browse files
committed
Fix typos and improve description
1 parent c4ac5f6 commit 04625bd

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -564,37 +564,52 @@ while (i < arr.length) {
564564
}
565565
sum
566566
```
567-
### Sowing meaningful definition names in quotes
567+
### Showing meaningful definition names in quotes
568568

569569
In the `powerCode` example above there is a `'{ val y = $x * $x; ... }` which when printed
570-
may show several different `val y = ...`. Even though there is no higene issue it may be hard
571-
to read the code. To overcome this each `y` can be assigned a meeningful name using the
572-
`scala.quoted.show.showName` annotation. For example `'{ @showName(${Expr("y" + i)}) val y = $x * $x; ... }`
573-
will assign to each `y` a name `y{i}` where `{i}` is a known String, if `i == 3` then it would be named `x3`.
570+
may show several different `val y = ...`.
571+
572+
For example
573+
```scala
574+
powerCode(16, '{7}).show
575+
```
576+
will show
577+
```scala
578+
val y: scala.Double = 7
579+
val y: scala.Double = y.*(y)
580+
val y: scala.Double = y.*(y)
581+
val y: scala.Double = y.*(y)
582+
val y: scala.Double = y.*(y)
583+
y
584+
```
585+
Even though there is no hygiene issue it may be hard to undestand the code. To overcome this inconvenience
586+
each `y` can be assigned a meaningful name using the `scala.quoted.show.showName` annotation.
587+
For example `'{ @showName(${Expr("y" + i)}) val y = $x * $x; ... }` will assign to each `y` a name
588+
`a{i}` where `{i}` is a known String, if `i == 3` then it would be named `a3`.
574589

575590
The `powerCode` can be defined as follows using `showName`
576591
```scala
577-
def powerCode(n: Long, x: Expr[Double]))(given QuoteContext): Expr[Double] = '{
578-
val x1 = $x
579-
${ powerCode(n, 2, 'x1) }
592+
def powerCodeD(n: Long, x: Expr[Double]))(given QuoteContext): Expr[Double] = '{
593+
val a1 = $x
594+
${ powerCodeD(n, 2, 'x1) }
580595
}
581-
def powerCode(n: Long, i: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
596+
def powerCodeD(n: Long, i: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
582597
if (n == 0) '{1.0}
583-
else if (n % 2 == 0) '{ @showName(${Expr("x" + i)}) val y = $x * $x; ${powerCode(n / 2, idx * 2, 'y)} }
584-
else '{ $x * ${powerCode(n - 1, idx, x)} }
598+
else if (n % 2 == 0) '{ @showName(${Expr("a" + i)}) val y = $x * $x; ${powerCodeD(n / 2, idx * 2, 'y)} }
599+
else '{ $x * ${powerCodeD(n - 1, idx, x)} }
585600
```
586601
then
587602
```scala
588-
powerCode(16, '{7}).show
603+
powerCodeD(16, '{7}).show
589604
```
590605
will show
591606
```scala
592-
val x1: scala.Double = 7
593-
val x2: scala.Double = x1.*(x1)
594-
val x4: scala.Double = x2.*(x2)
595-
val x8: scala.Double = x4.*(x4)
596-
val x16: scala.Double = x8.*(x8)
597-
x16
607+
val a1: scala.Double = 7
608+
val a2: scala.Double = a1.*(a1)
609+
val a4: scala.Double = a2.*(a2)
610+
val a8: scala.Double = a4.*(a4)
611+
val a16: scala.Double = a8.*(a8)
612+
a16
598613
```
599614

600615
### Find implicits within a macro

0 commit comments

Comments
 (0)