问题
I would like to restrict on which constant value extension function can be called. For example function like:
@IdRes
fun <T : View> Int.find() = findViewById<T>(this)
If this was called on real id, it's fine:
R.id.someView.find<TextView>() // ok
But this should make compilation error:
42.find<TextView>() // should be compile error
Is annotating extension receiver supported in Kotlin?
回答1:
As described in the documentation, you can use the following syntax:
fun @receiver:IdRes <T : View> Int.find() = ...
However, note that the Kotlin compiler is not aware of the semantics of the Android annotations, so their incorrect use is never a compilation error; it's at best a failed lint check.
来源:https://stackoverflow.com/questions/51156576/kotlin-annotate-receiver-of-extension-function