可以在这样一个带注释的类型上定义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)
}

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

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

I don't think that's possible yet using Kotlin, but would it be possible to add that feature in later Kotlin versions?

另一方面:

推荐答案

是的,你可以.使用以下语法:

@ColorInt
fun @receiver:ColorInt Int.darken(): Int {
    return ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
}

更多关于注释使用站点目标:http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets

Kotlin相关问答推荐

Kotlin和JavaFX:绑定行为奇怪

Kotlin中的增广赋值语句中的难以理解的错误

编译后的JavaFX应用程序立即以静默方式崩溃

为什么 <= 可以应用于 Int 和 Long,而 == 不能?

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

通过顺序多米诺骨牌操作列表管理对象的最佳方法是什么?

在 APK META-INF/library_release.kotlin_module 中复制的重复文件

Kotlin 中的 Java Scanner 相当于什么?

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

Kotlin 顶级函数与对象函数

TextField maxLength - Android Jetpack Compose

Android:在 DAO 中使用 Room 数据库和 LiveData 架构

Kotlin 扩展函数 - 覆盖现有方法

TornadoFX 中设置 PrimaryStage 或 Scene 属性的方法

Kotlin:使用自定义设置器时没有lateinit的解决方法?

在 kotlin 中,如何将主构造函数中的属性设置器设为私有?

Kotlin flatMap - map

Kotlin中的嵌套let块

如何在 IntelliJ IDEA 中禁用粘贴时将 Java 转换为 Kotlin?

Kotlin反射不可用