Created
May 17, 2020 22:52
-
-
Save yenerm/4ff23c907b56fe72f5b32e51a1ae0f8e to your computer and use it in GitHub Desktop.
Kotlin Reified
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
inline fun <reified T> calculate(value: Float): T { | |
return when (T::class) { | |
Float::class -> value as T | |
Int::class -> value.toInt() as T | |
else -> throw IllegalStateException("Only works with Float and Int") | |
} | |
} | |
val intCall: Int = calculate(123643) | |
val floatCall: Float = calculate(123643) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment