Skip to content

Commit 8fb7a44

Browse files
committed
Move showName to scala.internal
1 parent 04625bd commit 8fb7a44

File tree

4 files changed

+4
-51
lines changed

4 files changed

+4
-51
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -564,53 +564,6 @@ while (i < arr.length) {
564564
}
565565
sum
566566
```
567-
### Showing meaningful definition names in quotes
568-
569-
In the `powerCode` example above there is a `'{ val y = $x * $x; ... }` which when printed
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`.
589-
590-
The `powerCode` can be defined as follows using `showName`
591-
```scala
592-
def powerCodeD(n: Long, x: Expr[Double]))(given QuoteContext): Expr[Double] = '{
593-
val a1 = $x
594-
${ powerCodeD(n, 2, 'x1) }
595-
}
596-
def powerCodeD(n: Long, i: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
597-
if (n == 0) '{1.0}
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)} }
600-
```
601-
then
602-
```scala
603-
powerCodeD(16, '{7}).show
604-
```
605-
will show
606-
```scala
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
613-
```
614567

615568
### Find implicits within a macro
616569

library/src/scala/quoted/show/showName.scala renamed to library/src/scala/internal/quoted/showName.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package scala.quoted.show
1+
package scala.internal.quoted
22

33
/** Annotation used inside a quote to give a custom name to a definition.
44
* The `name` argument must be a literal String.

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ trait Printers
16421642

16431643
def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
16441644
val Annotation(ref, args) = annot
1645-
if (annot.symbol.owner.fullName == "scala.quoted.show.showName") this
1645+
if (annot.symbol.owner.fullName == "scala.internal.quoted.showName") this
16461646
else {
16471647
this += "@"
16481648
printTypeTree(ref)
@@ -1818,7 +1818,7 @@ trait Printers
18181818
}
18191819

18201820
private def splicedName(sym: Symbol)(given ctx: Context): Option[String] = {
1821-
sym.annots.find(_.symbol.owner.fullName == "scala.quoted.show.showName").flatMap {
1821+
sym.annots.find(_.symbol.owner.fullName == "scala.internal.quoted.showName").flatMap {
18221822
case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c)
18231823
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c)
18241824
case annot => None

tests/run-staging/quoted-show-name.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scala.quoted._
2-
import scala.quoted.show.showName
2+
import scala.internal.quoted.showName
33
import scala.quoted.staging._
44
import scala.reflect.ClassTag
55

0 commit comments

Comments
 (0)