I would find such a function really useful in stdl...
# stdlib
m
I would find such a function really useful in stdlib:
Copy code
inline fun <reified T : Enum<T>> enumValueOfOrNull(value: String): T? =
    enumValues<T>().find { it.toString() == value }
// or
inline fun <reified T : Enum<T>> enumValueOfOrNull(value: String): T? = try {
    enumValueOf<T>(value)
} catch (e: IllegalArgumentException) {
    null
}
👍 4
👍🏻 1
r
Agreed, although I'd prefer it in a static-like
Enum.valueOfOrNull()
style, like Java's
valueOf
. But that would need namespaces or something similar.
5