你如何定义在Scala中的@interface? [英] How do you define an @interface in Scala?
问题描述
怎样才能在斯卡拉创建一个 @interface
?老实说,我觉得自己很蠢问这个问题,但我找不到这个语法的随时随地的。我知道,你可以使用它们,但你怎么竟在Scala中定义新的?
Java的:
公共@interface MyAnnotation {}
斯卡拉:
???
这答案是基于Scala的2.8。
//将是Scala编译器可见的,但不是在类文件,也没有在运行。
//像RetentionPolicy.SOURCE
最后一类MyAnnotation扩展StaticAnnotation//将会看到存储在注解的类,但在运行时不会保留。
//这是一个实现的限制,设计应该支持
// RetentionPolicy.RUNTIME
最后一类MyAnnotation扩展ClassfileAnnotation
有关的详情,请参阅第11节中的<用户定义的注释 href=\"http://www.scala-lang.org/archives/downloads/distrib/files/nightly/pdfs/ScalaReference.pdf\">Scala参考
参见,例如:<一href=\"http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/annotation/tailrec.scala\">@tailrec.
更新的编译器警告说得好:
&GT;猫test.scala
最后一类MyAnnotation扩展scala.ClassfileAnnotation@MyAnnotation
一流的酒吧&GT; scalac test.scala
test.scala:1:警告:执行限制:继承的类文件不
让你的注释在运行时可见。如果这是
你想,你必须用Java编写注释类。
最后一类MyAnnotation扩展scala.ClassfileAnnotation
How does one create an @interface
in Scala? I honestly feel stupid asking that question, but I can't find the syntax for this anywhere. I know that you can consume them, but how do you actually define new ones in Scala?
Java:
public @interface MyAnnotation { }
Scala:
???
This answer is based on 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
For the full details, see section 11 "User Defined Annotations" in the Scala Reference See, for example: @tailrec.
UPDATE The compiler warning says it best:
>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中的@interface?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!