Colton Idle
04/26/2022, 6:42 PMenum class BackendEnv(val humanReadableName: String) {
STAGING("Staging"),
PROD("Production")
}
How would you do it?Colton Idle
04/26/2022, 6:43 PMinline fun <reified BackendEnv : Enum<BackendEnv>> listOfBackendEnv(): List<String> {
return enumValues<BackendEnv>().map { it.humanReadableName }
}
but it didn't work. The above code was inspired by this
https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constantsTim Oltjenbruns
04/26/2022, 6:46 PMBackendEnv.values().map { it.humanReadableName }
Tim Oltjenbruns
04/26/2022, 6:46 PMenumValues<>
is for generic accessColton Idle
04/26/2022, 6:57 PMephemient
04/26/2022, 7:09 PMinline fun <reified T : Enum<T>> enumNames() = enumValues<T>.map { it.name }
is where the generic version would be useful, but for your use case, it looks like you want that specific type, not a genericephemient
04/26/2022, 7:15 PMinterface HasHumanReadableName {
val humanReadableName: String
}
enum class BackendEnv(override val humanReadableName: String) : HasHumanReadableName {
STAGING("Staging"),
PROD("Production")
}
inline fun <reified T> listOfHumanReadableNames(): List<String>
where T : Enum<T>, T : HasHumanReadableName =
enumValues<T>().map { it.humanReadableName }
println(listOfHumanReadableNames<BackendEnv>())
for example