Last active
June 14, 2019 09:27
-
-
Save BurakDizlek/c43e95478ca5c309c34970b24dfff6de to your computer and use it in GitHub Desktop.
no enum constant for default safe enum value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : kotlin.Enum<T>> safeEnumValueOf(type: String?,defaultEnum:T): T { | |
return try { | |
java.lang.Enum.valueOf(T::class.java, type) | |
} catch (e: Exception) { | |
defaultEnum | |
} | |
} |
Hi , Yeah you are right. It can be T instead of T? .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome! I'd make default
T
, though, so a secondjava.lang.Enum.valueOf
call isn't needed in the exception handler, and the return type of the function can beT
instead ofT?
.