William Reed
09/30/2021, 1:37 PMval classes: List<KClass<*>> = listOf(
Person::class,
Teacher::class,
)
And this produces an error
val classes: List<KClass<Any>> = listOf(
Person::class,
Teacher::class,
)
(the difference being *
vs Any
)
I want to store any kclass, and don’t know the types. I thought *
meant they all have the same type but i don’t know what it isout Any
also seems to workmkrussel
09/30/2021, 1:55 PM*
projection works. https://typealias.com/guides/star-projections-and-how-they-work/
The entire series starting at https://typealias.com/guides/illustrated-guide-covariance-contravariance/ is very well done.William Reed
09/30/2021, 1:56 PMJoffrey
09/30/2021, 2:48 PMKClass<Any>
is the type of the Any
class, while KClass<*>
represents some class, but we don't know which one. If you want to store any KClass instance, you should use the star projectionKClass<out Any>
also works because it is the type of classes that are subtypes of Any
- so basically all of them.William Reed
09/30/2021, 3:05 PM