Why annotations written in Scala are not accessible at runtime?

六月ゝ 毕业季﹏ 提交于 2019-12-22 18:36:18

问题


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.

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?


回答1:


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, a Scala committer, explained on a post on the scala-language mailing list:

It's definitely non-trivial. Java annotations have specific representation in bytecode [...], which would require changes to Scala's bytecode generator.

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.



来源:https://stackoverflow.com/questions/38647213/why-annotations-written-in-scala-are-not-accessible-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!