https://kotlinlang.org logo
Title
b

burg

04/02/2018, 11:55 PM
is anyone able to help me understand this question I posted on SO? https://stackoverflow.com/questions/49620173/why-does-kotlin-kclasst-have-a-bound-generic-type-of-any
1
s

sandeep

04/03/2018, 6:03 AM
Assuming I understood the problem correctly, I think this is because KClass is defined as KClass<T : Any> and
Any
is inherited from
Any?
(?) at least as per the diagram below
ref:

http://natpryce.com/articles/000818/entire-hierarchy.png

I mean, with that diagram, it makes sense why
test2<String?>()
doesn’t work (as T is bounded by Any)
and the reason why
inline fun <reified T> test3(): KClass<T> = T::class // does not compile
doesn’t compile is since T is unbounded but the T of KClass is bounded by
Any
okay, just re-read your question on stack overflow. Your question was “why” and how to get it working. sorry, mis-read your question.
looks like a bug (or rather a limitation)
b

burg

04/03/2018, 3:48 PM
Yeah ultimately I am trying to have
String?::class
actually return the same as
String::class
(and it does, at some level, if you look at how test1 works in my example).
It does seem like an oversight, the bound itself is not really used, nor is it enforced in some cases.
l

louiscad

04/04/2018, 5:52 AM
null doesn't have a class