Reputation: 778
Suppose we have an abstract class
abstract class BaseFragment
Further, I would like to add a type parameter on the BaseFragment class, like so
abstract class BaseFragment<T>
Now, here is where I'm starting to run into problems - I want to put a restriction on T
, namely, I want T
to be a subtype of BaseFragment. However, when I write
abstract class BaseFragment<T: BaseFragment>
I get a compilation error:
One type argument expected for class BaseFragment
How do I resolve this? I've tried BaseFragment<out T: BaseFragment>
and
BaseFragment<T: BaseFragment<*>>
but that doesn't seem to work either...
Upvotes: 1
Views: 299
Reputation: 93511
You have to specify the type bounds of BaseFragment
. <*>
doesn't work, because then nothing can ever satisfy the condition. You can use
abstract class BaseFragment<T: BaseFragment<T>>
Subclasses can "pass the buck":
open class Foo<T: BaseFragment<T>>: BaseFragment<T>()
//or
open class Bar<T: Bar<T>>: BaseFragment<T>()
Or satisfy the subtype requirement themselves:
class Baz: BaseFragment<Baz>()
Upvotes: 1