是否可以像这样在注释类型上定义Kotlin扩展函数?注释类型的扩展函数
@ColorInt
fun @ColorInt Int.darken(): Int {
return ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
}
另一种形式:
@ColorInt
fun (@ColorInt Int).darken(): Int {
return ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
}
这将对应于以下静态funtion:
@ColorInt
fun darken(@ColorInt color: Int): Int {
return ColorUtils.blendARGB(color, Color.BLACK, 0.2f)
}
我不认为这是可能的但使用科特林,但它是可能的在后来的Kotlin版本中添加该功能?
在一个侧面说明: 同样的问题适用于@IntDef
,@StringDef
,@[resource type]Res
为好。
zsmb13有一个好点的解释,这可能会导致一些问题与具有相同签名一倍方法。无论如何,这可以通过为该函数注释一个不同的JVM方法名来解决。 – Heinrich