是否可以在Kotlin中将单个注释应用于多个使用场所目标? [英] Is it possible to apply a single annotation to multiple use-site targets in Kotlin?

查看:75
本文介绍了是否可以在Kotlin中将单个注释应用于多个使用场所目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档: 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?

我的用例是用SimpleXML的注释修饰类.要使用不可变的data class,您必须同时注释 字段和构造函数参数:

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)

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)

对于具有许多字段的数据类,您可以轻松获得3倍于实际代码的批注,并且消除重复将是一件很不错的事情.当您必须使用复杂的注释(例如ElementUnion,它本身可以是多行)时,这尤其烦人.

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.

推荐答案

不幸的是,从Kotlin 1.3开始,此用例没有语法

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

这篇关于是否可以在Kotlin中将单个注释应用于多个使用场所目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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