Improve internal representation of annotations when using defaults #22414
lrytz
started this conversation in
Feature Requests
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using defaults in an annotation, the
dotc.core.Annotation
representation represents missing arguments as calls to the corresponding$default$N
methods.Branch with test code: https://github.com/scala/scala3/compare/main...lrytz:scala3:sd884?expand=1
For
@ann(y = 22)
,annotation.arguments
isscala/scala#10976 is improving this for Scala 2. (That PR is still under review and may change.)
Default expression ASTs
ann
definition above becomes@ann(y = 22)
becomes@ann(x = 1, y = 22)
.User-defined annotation subclasses
The Scala 2 PR also introduces support for user-defined annoation subclasses. Example use case is
class nodep extends nowarn("cat=deprecation")
.To support that, a new method is added to the internal annotation representation:
def argsForSuper(parent: Symbol): List[Tree]
. Given an annotation@nodep def foo = ...
, the callnodepAnnot.argsForSuper(nowarnClassSymbol)
returnsList(Literal("cat=deprecation"))
.In order to support that, the argument expression AST passed to the super constructor are stored in a meta-annotation. The
nodep
definition above becomesThe implementation also supports forwarding parameters. For example
becomes
Runtime uses of annotation classes
Note that annotation classes in Scala can be used as ordinary executable code. In this case they must be compiled the same as other classes, for example the following needs to evaluate
22
before the default:this is correct in current Scala 3.
Beta Was this translation helpful? Give feedback.
All reactions