xin*_*aiz 4 android annotations kotlin kotlin-extension
我想限制可以调用哪些常量值扩展函数。例如功能如下:
@IdRes
fun <T : View> Int.find() = findViewById<T>(this)
Run Code Online (Sandbox Code Playgroud)
如果这是在真实 id 上调用的,那很好:
R.id.someView.find<TextView>() // ok
Run Code Online (Sandbox Code Playgroud)
但这应该会导致编译错误:
42.find<TextView>() // should be compile error
Run Code Online (Sandbox Code Playgroud)
Kotlin 是否支持注释扩展接收器?
fun @receiver:IdRes <T : View> Int.find() = ...
Run Code Online (Sandbox Code Playgroud)
但是请注意,Kotlin 编译器并不了解 Android 注释的语义,因此它们的错误使用绝不是编译错误;它充其量只是一次失败的 lint 检查。
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |