https://kotlinlang.org logo
Title
m

Massimo Carli

11/08/2018, 11:24 AM
Hi all, just a curiosity. Why the type of
String::class
is
KClass<String>
while the type of
"Hello"::class
is
KClass<out String>
? This means that what
"Hello"::class
returns is a
KClass<T>
where
T
is a subtype of
String
. But
String
is final and it doesn't have subtypes. 🤔
k

karelpeeters

11/08/2018, 12:03 PM
Typing is just not that smart.
m

Massimo Carli

11/08/2018, 3:24 PM
It's also funny that
val jClazz: Class<String> = String.javaClass
doesn't compile because it's expecting a
Class<String>
while
String.javaClass
is a
Class<String.Companion>
. When you remove the explicit type and get
val jClazz = String.javaClass
it compiles and the inferred type is....
Class<String>
.
😆 2