Scala中注释构造函数的参数 [英] Annotating constructor parameters in Scala

查看:216
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆