0

So my problem is simple, In java if i have a base class like this:

BaseViewModel.Java

public abstract class BaseViewModel<N> extends ViewModel {

I can extend this class to other classes without defining the generic argument N, like this:

public class BaseFragment<V extends BaseViewModel> { //this is fine

but kotlin throws an error with this approach asking for the generic definition.

class BaseFragment<V: BaseViewModel>: Fragment() {// one type argument expected

how to avoid this?

Abhishek Tiwari
  • 936
  • 10
  • 25

1 Answers1

3

Kotlin doesn't allow to use raw types as Java does. Thus, you have to specify some type for your V : BaseViewModel:

class BaseFragment<V: BaseViewModel<Any>>: Fragment() {

It is equivalent for your Java code cause V extends BaseViewModel basically means V extends BaseViewModel<Object>

hluhovskyi
  • 9,556
  • 5
  • 30
  • 42
  • That last depends on how you mean "basically". There are important differences. – Alexey Romanov May 28 '18 at 14:32
  • I don't know what author is trying to achieve since we have only class signatures. And I've posted answer only from perspective of that signatures. To see full picture and give more detailed answer we need more code samples which describes how `BaseViewModel` can be used. Anyway you've already posted nice link which explains such differences. – hluhovskyi May 28 '18 at 14:49
  • Maybe it makes sense to go with `V: BaseViewModel<*>` but again, I don't know whole picture. – hluhovskyi May 28 '18 at 14:50