Travis Griggs
03/15/2021, 7:20 PMval activity:KClass<FragmentActivity>
get() {
return when (this) {
Single, SingleWithSense, SingleWithPeriod, Dual, Echo -> AllocatedNodeActivity::class
Central -> CentralNodeActivity::class
Unknown -> PristineNodeActivity::class
}
}
It just yells at me because none of the return types match the KClass<FragmentActivity> type. What's the secret sauce I'm missing?Shawn
03/15/2021, 7:22 PMKClass<out FragmentActivity>
Travis Griggs
03/15/2021, 7:39 PMout
do for me in that context?Shawn
03/15/2021, 7:42 PMShawn
03/15/2021, 7:43 PMTravis Griggs
03/15/2021, 9:31 PMShawn
03/15/2021, 9:32 PMShawn
03/15/2021, 9:32 PMShawn
03/15/2021, 9:50 PMout
makes the parameter covariant, so you can use the type you specified and subtypes of it. This helps you because now you can safely opt into treating (for example) a KClass<PristineNodeActivity>
like a KClass<FragmentActivity>
We use in
and out
because Java’s wildcard projections (e.g. ? extends FragmentActivity
) are cumbersome, and we can relate the usage of the generic type to how we plan on using it (i.e. whether or not the object will consume (in) or produce (out) the typeTravis Griggs
03/15/2021, 10:20 PM