Answer the question
In order to leave comments, you need to log in
Kotlin: how to get a list of unknown Enum values?
In other words, how to implement the getEnumValues function:
fun getEnumValues(enumClass: KClass<Enum<*>>): Array<Enum<*>>{
...
}
Answer the question
In order to leave comments, you need to log in
I couldn't think of anything better than this :(
fun KClass<out Enum<*>>.enumValues(): Array<Enum<*>> {
@Suppress("UNCHECKED_CAST")
return this.java.getMethod("values").invoke(null) as Array<Enum<*>>
}
On the JVM, you can use java.lang.Class#getEnumConstants for this :
Under the hood, it uses the same reflection but caches the result.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question