小编Hei*_*ich的帖子

附加注释类型的扩展函数

是否可以在带注释的类型上定义Kotlin扩展函数?

@ColorInt
fun @ColorInt Int.darken(): Int {
    return ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
}
Run Code Online (Sandbox Code Playgroud)

替代形式:

@ColorInt
fun (@ColorInt Int).darken(): Int {
    return ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
}
Run Code Online (Sandbox Code Playgroud)

这将对应于以下静态功能:

@ColorInt
fun darken(@ColorInt color: Int): Int {
    return ColorUtils.blendARGB(color, Color.BLACK, 0.2f)
}
Run Code Online (Sandbox Code Playgroud)

我不认为这可能还在使用Kotlin,但是可以在以后的Kotlin版本中添加该功能吗?

在一个侧面说明:同样的问题也适用于@IntDef,@StringDef,@[resource type]Res以及.

annotations kotlin kotlin-extension

6
推荐指数
1
解决办法
794
查看次数

标签 统计

annotations ×1

kotlin ×1

kotlin-extension ×1