如何在 Scala 中创建@interface
?老实说,我觉得问这个问题很愚蠢,但我在任何地方都找不到这个语法。我知道您可以使用它们,但是如何在 Scala 中实际定义新的?
Java:
public @interface MyAnnotation { }
斯卡拉:
???
最佳答案
此答案基于 Scala 2.8。
// Will be visible to the Scala compiler, but not in the class file nor at runtime.
// Like RetentionPolicy.SOURCE
final class MyAnnotation extends StaticAnnotation
// Will be visible stored in the annotated class, but not retained at runtime.
// This is an implementation restriction, the design is supposed to support
// RetentionPolicy.RUNTIME
final class MyAnnotation extends ClassfileAnnotation
有关完整详细信息,请参阅 Scala Reference 中的第 11 节“用户定义的注释” 例如,参见:@tailrec .
更新编译器警告说明了这一点:
>cat test.scala
final class MyAnnotation extends scala.ClassfileAnnotation
@MyAnnotation
class Bar
>scalac test.scala
test.scala:1: warning: implementation restriction: subclassing Classfile does not
make your annotation visible at runtime. If that is what
you want, you must write the annotation class in Java.
final class MyAnnotation extends scala.ClassfileAnnotation
关于scala - 如何在 Scala 中定义 @interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2265773/