Skullper
Skullper

Reputation: 775

Generics and abstract class in Kotlin

I have a base abstract class:

abstract class BaseFragment<T : BasePresenter> : Fragment(){

    protected var presenter : T? = null

    abstract fun providePresenter() : T

    abstract fun getLayoutId() : Int

    abstract fun onCreate()

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val root = inflater?.inflate(getLayoutId(), container, false)
        presenter = providePresenter()
        return root
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        onCreate()
    }

}

My code in JAVA:

//JAVA

private ArrayList<BaseFragment> fragments;

How can I use it Kotlin? Code bellow not working

//Kotlin

val tabs = ArrayList<BaseFragment>() //error: One type argument expected for class BaseFragment<T : BasePresenter> : Fragment

Upvotes: 1

Views: 1922

Answers (1)

Lukas Lechner
Lukas Lechner

Reputation: 8191

You need to specify a type for your T

val tabs = ArrayList<BaseFragment<SomePresenterType>>()

Upvotes: 2

Related Questions