I’m looking if it is possible to use reified for class instantiation. I tried code like:
Copy code
class BaseFragment<T: BaseViewModel> {
val viewModel: T = createViewModel()
}
private fun <reified T: BaseViewModel> createViewModel() = T::java.class.newInstance()
This compiles but I have just one function with BaseViewModel class as argument. Is it possible to get many such methods for every class that extends it with more specific view model?
f
FunkyMuse
12/30/2020, 7:56 AM
You can't use reified at a class level generic type.
However you can make one abstract/open class and every other that implements it will have that method available.
e
Eugen Martynov
12/30/2020, 9:09 PM
Do you have a reference or example?
Eugen Martynov
12/30/2020, 9:10 PM
I wanted to avoid to write in every subclass method to provide specific class name