Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reified types parameters in Kotlin
// single param
inline fun <reified T> Any.isInstanceOf(): Boolean = this is T
fun main() {
val isStringAString = "String".isInstanceOf<String>()
val isIntAString = 1.isInstanceOf<String>()
}
// multiple params
inline fun <reified T, reified U> haveSameType(first: T, second: U) =
first is U && second is T
// extension properties, but the type parameter is used as the receiver type
inline val <reified T> T.theClass
get() = T::class.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.