Reputation: 31
I have an extensive class hierarchy that I want to map to an existing enum type. Given this example hierarchy:
open class A {
open class BB: A() {
class CCC: BB(){}
class DDD: BB(){}
}
}
I want to map the class type from the hierarchy to the enum, i.e. in kotlin a mapping of type
val mapping: Pair<KClass<A>, TypeEnum> = A::class to TypeEnum.TYPEA
I thought to use a sealed class for each hierarchy class that contains the corresponding mapping, I have this code, but isn't working as the compiler says I can't instantiate a sealed class
sealed class ASealed(type: KClass<out A>) {
abstract val typeMapping: TypeEnum
sealed class BBSealed(type: KClass<out BB>) : ASealed(type) {
override val typeMapping = TypeEnum.TYPEBB
sealed class CCCSealed(type: KClass<CCC>) : BBSealed(type){
override val typeMapping = TypeEnum.TYPECCC
}
sealed class DDDSealed(type: KClass<DDD>) : BBSealed(type){
override val typeMapping = TypeEnum.TYPEDDD
}
}
}
fun mapto(){
val ccc = CCC()
val enum = ASealed(ccc::class).typeMapping
}
Is it possible to use this approach, so I have all the class hierarchy grouped and retrieve the enum type like in the above function?
Upvotes: 0
Views: 306