Consider the below code:
class SegmentController<E : Enum<E>>() {
fun getEnumForOrdinal(ordinal: Int) : E {
//values is undefined?
//how can I return the enum for the specified ordinal value?
return E.values()[ordinal]
}
fun getOrdinalForEnum(enum: E): Int {
//no problem getting the ordinal from the supplied E enum
return enum.ordinal
}
}
Its a generic class, taking any Enum as a template.
- How can I return an Enum value based on on ordinal in
getEnumForOrdinal()
? - I would expect the
E.values()
to be available, but its not inside the generic class?
I did my research but cannot find any solution based on the ordinal, links I found: