I have Sealed Class like below.
sealed class Number {
object One: Number() {
}
object Two: Number() {
}
object Three: Number() {
}
object Four: Number() {
}
}
then, I want to use One, and Two using sealedSubclasses.
// this code compile error. `Operator '==' cannot be applied to 'KClass<out KClass<out Number>>' and 'Number.One'`
val subClasses = Number::class.sealedSubclasses.filter { clazz -> clazz::class == Number.One }
Can I solve this? Do you know any Idea?
CodePudding user response:
Use clazz == Number.One::class.
Currently, you’re using clazz::class which is KClass<out KClass<out Number>>, the class of what was already a class.
And you forgot the ::class at the end.
However, you’re filtering to find out which subclass is the class you already know so there’s no point. You could just replace you’re whole line of code with val subclasses = listOf(Number.One::class).
