是否可以像這樣在註釋類型上定義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