shouldn't `inline fun <reified T> foo() = T:...
# announcements
r
shouldn't
inline fun <reified T> foo() = T::class.qualifiedName
be disallowed if
inline fun <reified T> foo() = T::class
is?
s
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
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
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
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
I noticed that, unsure how that resolves exactly
especially since
Int?::class
won’t compile
r
it looks a bit like a bug no?
s
honestly I don’t know enough to say