Created
July 17, 2019 14:15
-
-
Save shahsurajk/ba15bf7bb4318b2b2c106d7cada8eaa5 to your computer and use it in GitHub Desktop.
Reified inline function with parameter
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> 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