Skip to content

Instantly share code, notes, and snippets.

@pambrose
Created December 8, 2019 02:31
Show Gist options
  • Save pambrose/17fb91993ab0a13a7148c999cffde13b to your computer and use it in GitHub Desktop.
Save pambrose/17fb91993ab0a13a7148c999cffde13b to your computer and use it in GitHub Desktop.
Return type parameter count
val Any.typeParameterCount: Int
get() =
when {
this is Array<*> -> 1
!javaClass.genericSuperclass.javaClass.kotlin.isSubclassOf(ParameterizedType::class) -> 0
else -> (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.size
}
4.typeParameterCount shouldEqual 0
"dd".typeParameterCount shouldEqual 0
listOf(3).typeParameterCount shouldEqual 1
mapOf(3 to "d").typeParameterCount shouldEqual 2
arrayOf(4).typeParameterCount shouldEqual 1
(IntArray(1) { 2 }).typeParameterCount shouldEqual 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment