-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add customizable names for definitions in quotes #7346
Conversation
391c576
to
ff54544
Compare
This allow naming val/def in quoted code with computed names
ff54544
to
c4ac5f6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, LGTM
* myVal + 1 | ||
* ``` | ||
*/ | ||
class showName(name: String) extends scala.annotation.Annotation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: what about displayName
or debugName
? showName
feels more like a verb.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose show name because only the Expr.show
will display it. Compiler debugging options as -Xprint
will just print the annotations.
8a948bb
to
04625bd
Compare
} | ||
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("a" + i)}) val y = $x * $x; ${powerCodeD(n / 2, idx * 2, 'y)} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead provide a means to developers for generating val definitions?
else if (n % 2 == 0) '{ @showName(${Expr("a" + i)}) val y = $x * $x; ${powerCodeD(n / 2, idx * 2, 'y)} } | |
else if (n % 2 == 0) { | |
val y = named("a" + i, '{ $x * $x}) | |
'{ $y; ${powerCodeD(n / 2, idx * 2, y)} } | |
} |
Also, I’m very surprised that there is no name clash in the expanded code you shown. It seems that all the lines of the block are in the same lexical scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I’m not sure about the suggestion above. It can not express something like '{ val MyExtractor(y) = $x * $x }
, for instance.
Otherwise, could we support something like the following?
else if (n % 2 == 0) '{ @showName(${Expr("a" + i)}) val y = $x * $x; ${powerCodeD(n / 2, idx * 2, 'y)} } | |
else if (n % 2 == 0) { | |
val y = name("a" + i) | |
'{ val $y = $x * $x; ${powerCodeD(n / 2, idx * 2, y)} } | |
} |
I believe we can’t, though…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annotation is the first step towards splicing a name. This PR is a starting point to support all those patterns. Ideally a '{ val $y = ... }
will be desugared to '{ @showName(${Expr(y.toString)}) val y = ... }
9450b69
to
2dd7c32
Compare
2dd7c32
to
8fb7a44
Compare
@liufengyun I extracted the second part of this PR in #7388. This PR only contains the core/internal logic required to splice names in quotes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
I'm happy to see showName
now is in scala.internal
.
This allow naming val/def in quoted code with computed names