For an enum, when doing `MyEnum.valueOf(someString...
# stdlib
s
For an enum, when doing
MyEnum.valueOf(someString)
to get an enum from its String representation, what is the equivalent when using the new
entries
field? Is it perhaps
MyEnum.entries.first { it.name == someString }
? With
firstOrNull
being a safe alternative to it? Or am I missing something?
👀 1
d
You can still use
valueOf(name)
entries
is a replacement for
values()
, not for
valueOf
But if you want to write it in the safe way it's better to use
entries + firstOrNull
instead of
valueOf + catch (e: IllegalArgumentException)
🌟 1
s
Yeap, I was trying to go for a safe approach here hence me trying to look for alternatives to
valueOf()
. And if I can avoid a try/catch I’m more than happy to do so. Thanks a lot for the help!
e