Skip to content

Commit

Permalink
Fix typos and improve description
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Oct 7, 2019
1 parent c4ac5f6 commit 8a948bb
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,37 +564,52 @@ while (i < arr.length) {
}
sum
```
### Sowing meaningful definition names in quotes
### Showing meaningful definition names in quotes

In the `powerCode` example above there is a `'{ val y = $x * $x; ... }` which when printed
may show several different `val y = ...`. Even though there is no higene issue it may be hard
to read the code. To overcome this each `y` can be assigned a meeningful name using the
`scala.quoted.show.showName` annotation. For example `'{ @showName(${Expr("y" + i)}) val y = $x * $x; ... }`
will assign to each `y` a name `y{i}` where `{i}` is a known String, if `i == 3` then it would be named `x3`.
may show several different `val y = ...`.

For example
```scala
powerCode(16, '{7}).show
```
will show
```scala
val y: scala.Double = 7
val y: scala.Double = y.*(y)
val y: scala.Double = y.*(y)
val y: scala.Double = y.*(y)
val y: scala.Double = y.*(y)
y
```
Even though there is no hygiene issue it may be hard to undestand the code. To overcome this inconvenience
each `y` can be assigned a meaningful name using the `scala.quoted.show.showName` annotation.
For example `'{ @showName(${Expr("y" + i)}) val y = $x * $x; ... }` will assign to each `y` a name
`a{i}` where `{i}` is a known String, if `i == 3` then it would be named `a3`.

The `powerCode` can be defined as follows using `showName`
```scala
def powerCode(n: Long, x: Expr[Double]))(given QuoteContext): Expr[Double] = '{
val x1 = $x
def powerCodeD(n: Long, x: Expr[Double]))(given QuoteContext): Expr[Double] = '{
val a1 = $x
${ powerCode(n, 2, 'x1) }
}
def powerCode(n: Long, i: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
def powerCodeD(n: Long, i: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
if (n == 0) '{1.0}
else if (n % 2 == 0) '{ @showName(${Expr("x" + i)}) val y = $x * $x; ${powerCode(n / 2, idx * 2, 'y)} }
else '{ $x * ${powerCode(n - 1, idx, x)} }
else if (n % 2 == 0) '{ @showName(${Expr("a" + i)}) val y = $x * $x; ${powerCodeD(n / 2, idx * 2, 'y)} }
else '{ $x * ${powerCodeD(n - 1, idx, x)} }
```
then
```scala
powerCode(16, '{7}).show
powerCodeD(16, '{7}).show
```
will show
```scala
val x1: scala.Double = 7
val x2: scala.Double = x1.*(x1)
val x4: scala.Double = x2.*(x2)
val x8: scala.Double = x4.*(x4)
val x16: scala.Double = x8.*(x8)
x16
val a1: scala.Double = 7
val a2: scala.Double = a1.*(a1)
val a4: scala.Double = a2.*(a2)
val a8: scala.Double = a4.*(a4)
val a16: scala.Double = a8.*(a8)
a16
```

### Find implicits within a macro
Expand Down

0 comments on commit 8a948bb

Please sign in to comment.