Created
April 9, 2019 10:09
-
-
Save zamahaka/0f58f535a7e471859aade6c8e1059c0f to your computer and use it in GitHub Desktop.
This file contains 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
interface HasX { | |
val x: Int | |
companion object | |
} | |
enum class A(override val x: Int) : HasX { ONE(1), TWO(2) } | |
enum class B(override val x: Int, val w: String) : HasX { AB(2, "ab"), CDE(3, "cde") } | |
inline fun <reified T> KClass<T>.findX(x: Int): T? where T : HasX, T : Enum<T> { | |
return enumValues<T>().find { it.x == x } | |
} | |
inline fun <reified T> findXOfEnum(x: Int): T? where T : HasX, T : Enum<T> { | |
return enumValues<T>().find { it.x == x } | |
} | |
inline fun <reified T> HasX.Companion.findXOfEnum(x: Int): T? where T : HasX, T : Enum<T> { | |
return enumValues<T>().find { it.x == x } | |
} | |
fun foo() { | |
val classA = A::class.findX(2) | |
val a = findXOfEnum<A>(2) | |
val b = findXOfEnum<B>(3) | |
val hax = HasX.findXOfEnum<A>(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment