Skip to content

Instantly share code, notes, and snippets.

@shahsurajk
Created July 17, 2019 14:15
Show Gist options
  • Save shahsurajk/ba15bf7bb4318b2b2c106d7cada8eaa5 to your computer and use it in GitHub Desktop.
Save shahsurajk/ba15bf7bb4318b2b2c106d7cada8eaa5 to your computer and use it in GitHub Desktop.
Reified inline function with parameter
inline fun <reified T> myGenericFunction(value: T): T {
// we can't do this without marking the parameter 'T' as reified and the function as inline.
val whoAmI = when (T::class.java.simpleName) {
Int::class.java.simpleName -> {
"I am an Int"
}
String::class.java.simpleName -> {
"I am Unknown"
}
else -> "I am Groot!"
}
println(whoAmI)
return value
}
fun main() {
val a = myGenericFunction("String") // String
val b = myGenericFunction(1) // Int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment