Why it does not allow to assign descendant class ?...
# announcements
o
Why it does not allow to assign descendant class ?
Copy code
interface TopicDelegate<T: ProfileItem> {
    val title: Int
}

class DistrictDelegate(val api: INetworkApi): TopicDelegate<District> {
    override val title: Int = R.string.choose_districts
}

abstract class TopicFragment<T: ProfileItem> : BaseFragment() {
    private var modeDelegate: TopicDelegate<T> = DistrictDelegate(mNetworkApi) //does not work
    //Type mismatch. Required: TopicDelegate<T> Found: DistrictDelegate
}
d
DistrictDelegate
is a
TopicDelegate<District>
, but a
TopicDelegate<T>
is required.
T
might not be
District
.
o
District extends ProfileItem
so
TopicDelegate<District>
should fit into
TopicDelegate<T: ProfileItem>
right ?
d
No.
T
is a parameter, it may stand for anything that extends
ProfileItem
. So if
T
is actually a
SomeOtherProfileItem
, you can't put a
District
in there.