加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

你如何在Scala中定义一个@interface?

发布时间:2020-12-16 09:37:58 所属栏目:安全 来源:网络整理
导读:如何在Scala中创建一个@interface?我真的觉得愚蠢的问这个问题,但是我找不到任何地方的语法。我知道你可以消耗它们,但是如何在Scala中实际定义新的? Java的: public @interface MyAnnotation { } 斯卡拉: ??? 解决方法 这个答案是基于Scala 2.8的。 //
如何在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读