Skip to content

Instantly share code, notes, and snippets.

@kwmt
Last active November 17, 2017 08:31
Show Gist options
  • Save kwmt/2da23dfa8615ae4f87a5000c014a5137 to your computer and use it in GitHub Desktop.
Save kwmt/2da23dfa8615ae4f87a5000c014a5137 to your computer and use it in GitHub Desktop.
inline fun <reified T: Enum<T>> String.enumValueOfOrNull() : T? {
return enumValues<T>().find { it.name == this }
}
enum class SampleEnum(val code:Int) {
Ja(1), En(2)
}
fun main(args: Array<String>) {
val sampleString = "Ja"
val sampleEnum = sampleString?.enumValueOfOrNull<SampleEnum>()
if (sampleEnum == null) {
print("sampleEnum is null")
}
print(sampleEnum.code)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment