Scala中注释构造函数的参数 [英] Annotating constructor parameters in Scala
问题描述
注解构造函数的参数,似乎当编译成字节code什么也不做。我没有得到任何编译器警告或者
Annotating constructor parameters seems to do nothing when compiled to bytecode. I get no compiler warnings either.
以下的作品。 getAnnotations
为名称
现场退货 javax.annotation.Nullable
The following works. getAnnotations
for the name
field returns javax.annotation.Nullable
.
class Person {
@Nullable var name: String = _;
}
下列不,既不符合 VAL
或 VAR
。
class Person(@Nullable var name: String)
这是可能不是故意的,所以有我丢失的东西,或者我应该去提交错误报告?
This is probably not intentional, so is there something I am missing or should I go file a bug report?
推荐答案
您需要指定当你在构造函数的参数指定的注解什么应该得到注解。
You need to specify what should get annotated when you specify annotations on constructor parameters.
要做到这一点从 scala.annotation.target
注释与一个或多个注释您的注释,例如的getter
,二传手
或你的情况字段
:
To do that annotate your annotation with one ore more annotations from scala.annotation.target
, e.g. getter
, setter
or as in your case field
:
import annotation.target.field
class Person(@(Nullable @field) var name: String)
您也可以使用类型别名为:
You can also use type aliases for that:
type NullableField = Nullable @field
class Person(@NullableField var name: String)
这篇关于Scala中注释构造函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!