Created
December 8, 2019 02:31
-
-
Save pambrose/17fb91993ab0a13a7148c999cffde13b to your computer and use it in GitHub Desktop.
Return type parameter count
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
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