-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customizable names for definitions in quotes
This allow naming val/def in quoted code with computed names
- Loading branch information
1 parent
736af36
commit c4ac5f6
Showing
5 changed files
with
116 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package scala.quoted.show | ||
|
||
/** Annotation used inside a quote to give a custom name to a definition. | ||
* The `name` argument must be a literal String. | ||
* | ||
* Usage: | ||
* ```scala | ||
* def let(name: String)(value: Expr[Int])(in: Expr[Int] => Expr[Int]): Expr[Int] = '{ | ||
* @showName(${Expr(name)}) | ||
* val x = $value | ||
* ${ in('x) } | ||
* } | ||
* ``` | ||
* then using it in | ||
* ```scala | ||
* let("myVal")('{4})(x => '{ $x + 1}).show | ||
* ``` | ||
* will retuns the code | ||
* ```scala | ||
* val myVal = 4 | ||
* myVal + 1 | ||
* ``` | ||
*/ | ||
class showName(name: String) extends scala.annotation.Annotation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
((x1: scala.Double) => x1.*({ | ||
val x2: scala.Double = x1.*(x1) | ||
val x4: scala.Double = x2.*(x2) | ||
x4.*({ | ||
val x8: scala.Double = x4.*(x4) | ||
x8.*({ | ||
val x16: scala.Double = x8.*(x8) | ||
val x32: scala.Double = x16.*(x16) | ||
val x64: scala.Double = x32.*(x32) | ||
x64 | ||
}) | ||
}) | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.quoted._ | ||
import scala.quoted.show.showName | ||
import scala.quoted.staging._ | ||
import scala.reflect.ClassTag | ||
|
||
object Test { | ||
given Toolbox = Toolbox.make(getClass.getClassLoader) | ||
def main(args: Array[String]): Unit = withQuoteContext { | ||
println(powerCode(77).show) | ||
} | ||
|
||
def powerCode(n: Long)(given QuoteContext): Expr[Double => Double] = | ||
'{ x1 => ${powerCode(n, 2, 'x1)} } | ||
|
||
def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] = | ||
if (n == 0) '{1.0} | ||
else if (n == 1) x | ||
else if (n % 2 == 0) '{ @showName(${Expr("x" + idx)}) val y = $x * $x; ${powerCode(n / 2, idx * 2, '{y})} } | ||
else '{ $x * ${powerCode(n - 1, idx, x)} } | ||
|
||
} |