https://kotlinlang.org logo
#getting-started
Title
# getting-started
g

Greg Rynkowski

01/26/2022, 10:16 AM
Question about generics. Having this code:
Copy code
// all T - java classes implementing ViewDataBinding, each providing a static `bind` method
// ViewDataBinding - a Java abstract class (with no declaration of `bind`)

interface BindingAware<T : ViewDataBinding> {
    var f: T

    fun bind(v: View) {
        // f = T.bind(a)  // <= how to call static method provided by class behind T?
    }
}
How to call static method of T, having only T?
r

Rob Elliot

01/26/2022, 10:32 AM
I don't see how you can. Firstly static methods are not part of the signature of a class in that way - you can't declare an abstract static method on
ViewDataBinding
. But even if
bind
were an instance method, if it wasn't defined on
ViewDataBinding
then it would be purely convention that all subclasses of
ViewDataBinding
had one, not something the static type checker could rely on and prove. I see this is an Android class - might be worth asking in #android or even #android-databinding how people have solved what you are trying to do?
2 Views