Skip to content

Instantly share code, notes, and snippets.

@BurakDizlek
Last active June 14, 2019 09:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurakDizlek/c43e95478ca5c309c34970b24dfff6de to your computer and use it in GitHub Desktop.
Save BurakDizlek/c43e95478ca5c309c34970b24dfff6de to your computer and use it in GitHub Desktop.
no enum constant for default safe enum value
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
}
}
@theartofme
Copy link

This is awesome! I'd make default T, though, so a second java.lang.Enum.valueOf call isn't needed in the exception handler, and the return type of the function can be T instead of T?.

@BurakDizlek
Copy link
Author

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