Annotating constructor parameters in Scala

前端 未结 1 1976
感动是毒
感动是毒 2021-02-01 17:11

Annotating constructor parameters seems to do nothing when compiled to bytecode. I get no compiler warnings either.

The following works. getAnnotations for

1条回答
  •  -上瘾入骨i
    2021-02-01 17:47

    You need to specify what should get annotated when you specify annotations on constructor parameters.

    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)
    

    Update Scala 2.12

    Now this specific annotation and others are in the package scala.annotation.meta rather than scala.annotation.target

    import scala.annotation.meta.{field, param}
    

    0 讨论(0)
提交回复
热议问题