https://kotlinlang.org logo
#announcements
Title
# announcements
r

robstoll

06/17/2019, 4:47 PM
shouldn't
inline fun <reified T> foo() = T::class.qualifiedName
be disallowed if
inline fun <reified T> foo() = T::class
is?
s

Shawn

06/17/2019, 4:50 PM
It’s not disallowed per se, the generics just make things a bit more complicated
Copy code
inline fun <reified T : Any> foo(): KClass<T> = T::class
is what you want because
KClass<T>
is explicitly bounded to
Any
r

robstoll

06/17/2019, 4:51 PM
exactly, so why is this ignored in
inline fun <reified T> foo() = T::class.qualifiedName
or to ask differently what is the type of
T::class
and how can I use it in other code 😄
s

Shawn

06/17/2019, 4:52 PM
because the return type is inferred as
String?
and you don’t need to infer a projection or bound for
T
in the return type
the type of
T::class
is
KClass<T>
r

robstoll

06/17/2019, 4:54 PM
I don't think so, as
KClass<T>
requires
T
to have an upper bound of
Any
-- it's unsafe to ignore that no?
Copy code
inline fun <reified T> foo() : String? {
    val a: KClass<T> = T::class
    return a.qualifiedName
}
Does not compile
btw.
foo<Int?>()
returns
<http://kotlin.Int|kotlin.Int>
, no mentioning of the nullable part
s

Shawn

06/17/2019, 5:01 PM
I noticed that, unsure how that resolves exactly
especially since
Int?::class
won’t compile
r

robstoll

06/17/2019, 5:02 PM
it looks a bit like a bug no?
s

Shawn

06/17/2019, 5:10 PM
honestly I don’t know enough to say