I have created extension method:
@Suppress("UNCHECKED_CAST")
operator fun <T : View> View.get(@IdRes id:Int): T =
this.findViewById(id) as T
Main usage of this method:
class A {
lateinit var text: TextView
fun init(view:View) {
text = view[R.id.text]
}
}
This works perfectly, but when I try use it without variable:
fun test() {
view[R.id.text].visibility = View.GONE // error
}
Error:
Type inference failed: Not enough information to infer parameter T in
operator fun <T : View> View.get(id: Int): T
Please specify it explicitly.
If I write the analog code in java, methods of class View available without direct specification of View type.
Is it possible in kotlin? Maybe do some changes in signature of generic type somehow?