Skip to content

Instantly share code, notes, and snippets.

@yenerm
Created May 17, 2020 22:52
Show Gist options
  • Save yenerm/4ff23c907b56fe72f5b32e51a1ae0f8e to your computer and use it in GitHub Desktop.
Save yenerm/4ff23c907b56fe72f5b32e51a1ae0f8e to your computer and use it in GitHub Desktop.
Kotlin Reified
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