为什么在运行时无法访问用Scala编写的注释? [英] Why annotations written in Scala are not accessible at runtime?
问题描述
我在scalatest文档中遇到了一条声明:
I have come across a statement in scalatest documentation:
It must be defined in Java instead of Scala so it will be accessible at runtime.
这很奇怪,因为我当时觉得Scala注释比Java注释更强大.为什么缺少此功能?有计划将其重新添加吗?
This is quite weird as I was under the impression that Scala annotation is stronger than its Java counterpart. Why this feature is missing? Is there a plan to add it back?
推荐答案
目前尚无计划在运行时保留Scala支持注释(这个问题已经有一段时间了,但名称为
There is no known plan to have Scala support annotations retained at runtime (the issue has been known for a while but as this page says, it won't be fixed).
Lukas Rytz ,Scala提交者,在
Lukas Rytz, a Scala committer, explained on a post on the scala-language
mailing list:
这绝对是不平凡的. Java注释具有特定的 字节码表示形式[...], 这将需要更改Scala的字节码生成器.
It's definitely non-trivial. Java annotations have specific representation in bytecode [...], which would require changes to Scala's bytecode generator.
Java注释的语言也很特殊,与 Java语言的其余部分,因此需要定义 如何在Scala类中表示这一点.
Also the language of java annotations is quite special, different from the rest of the Java language [...], so one would need to define how this can be represented in a Scala class.
这篇关于为什么在运行时无法访问用Scala编写的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!