Colton Idle
05/19/2023, 9:08 PMval banana = Type.getEnumFor("one")
enum class Type(val stringly: String) {
A("one"),
B("two"),
C("three")
}
and so banana would be == Type.A?Paul Griffith
05/19/2023, 9:10 PMephemient
05/19/2023, 9:11 PMType.valueOf("one")
enumValueOf<Type>("one")
built-inephemient
05/19/2023, 9:12 PMColton Idle
05/19/2023, 9:14 PMephemient
05/19/2023, 9:14 PMval valuesByStringly = values.associateBy { it.stringly }
valuesByStringly["one"]ephemient
05/19/2023, 9:14 PMPaul Griffith
05/19/2023, 9:16 PMvalues() (iterates a new array each time) and iterating the list
in practice unless this is called in a really hot loop it’s likely not going to matter much but it’s still good practice
I think kotlin 1.9 or 2.0 is supposed to have an alternate method that doesn’t create the new array for values() each timeArtem Kobzar
05/19/2023, 9:17 PMentries static propertyephemient
05/19/2023, 9:18 PMephemient
05/19/2023, 9:19 PMColton Idle
05/19/2023, 9:20 PMtry {
RollerToaster.Type.valueOf(this.input.type.toUppercase())
} catch (e: IllegalArgumentException) {
Type.UNKNOWN
}Colton Idle
05/19/2023, 9:20 PMDetekt - SwallowedException: The caught exception is swallowed. The original exception could be lost.ephemient
05/19/2023, 9:25 PMAll further potential extensions, such as,valueOfOrNull(String)can be implemented on the standard library level as members or extensions overhasValue(String).EnumEntries
should get better in the future
Colton Idle
05/19/2023, 9:32 PMMatteo Mirk
05/22/2023, 3:50 PMrunCatching {
RollerToaster.Type.valueOf(...)
}.getOrElse(Type.UNKNOWN)ephemient
05/22/2023, 4:20 PMrunCatching is the wrong solution. if there's a ExceptionInInitializerError (for example) that should be propagatedMatteo Mirk
05/23/2023, 8:48 AMephemient
05/23/2023, 9:08 AMExceptionInInitializerError, your whole enum class is broken and none of its values will work. there is zero reason to swallow it and allow execution to continue.ephemient
05/23/2023, 9:12 AMrunCatching are meant to allow you to capture exceptions for later handling, not to drop them. https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md#use-cases