Is it possible to apply a single annotation to multiple use-site targets in Kotlin?

99封情书 提交于 2019-11-28 07:31:19

问题


According to the documentation: https://kotlinlang.org/docs/reference/annotations.html

You can apply multiple annotations to a single site-use target, but is there a way to apply the same annotation to multiple site-use targets?

My use-case is decorating classes with annotations for SimpleXML. To use an immutable data class, you have to annotate both the field and the constructor parameter:

data class Data( @field:Element(name = "ID") @param:Element(name = "ID") val id: Int, @param:Element(name = "TEXT") @field:Element(name = "TEXT") val text: String)

For data classes with many fields, you can easily end up with 3x as many annotations as actual code, and it would be nice to eliminate the duplication. This is especially annoying when you have to use a complicated annotation like ElementUnion which can be multiple lines on its own.


回答1:


Unfortunately, as of Kotlin 1.3, there's no syntax for this use case



来源:https://stackoverflow.com/questions/47082194/is-it-possible-to-apply-a-single-annotation-to-multiple-use-site-targets-in-kotl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!