gpt4 book ai didi

scala - 如何在 Scala 中定义 @interface?

转载 作者:行者123 更新时间:2023-12-02 21:11:42 25 4
gpt4 key购买 nike

如何在 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

Discussion

关于scala - 如何在 Scala 中定义 @interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2265773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com