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

karelpeeters

11/24/2019, 6:21 PM
Is there any way to call
bar
here?
Copy code
inline fun <reified A: Any?> foo() {
    bar(A::class)
}

fun <B: Any> bar(cls: KClass<B>) {}
This should be possible,
A::class
always returns the proper class, even if
A
is nullable. It looks like the compiler doesn't realize that (I'll probably report this on youtrack). Usually I'd just cast something but I'm not sure how to do that here, I want to cast to
A!!
but that's not real syntax of course. Any tricks?
a

Alowaniak

11/24/2019, 6:38 PM
Can't you do
bar(A::class as KClass<Any>)
?
k

karelpeeters

11/24/2019, 6:39 PM
Hmm that does work, but then I completely lose all type information and I need to cast the result back as well.
k

Kroppeb

11/24/2019, 7:20 PM
You aren't using the nullability of
A
anywhere in this snippet. How are you using it?
☝🏼 3
k

karelpeeters

11/24/2019, 7:36 PM
Huh it turns out I indeed do not need
A
to be nullable in my real use case!