-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add customizable names for definitions in quotes #7346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: what about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose show name because only the |
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 | ||
}) | ||
}) | ||
})) |
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)} } | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.