https://kotlinlang.org logo
#stdlib
Title
# stdlib
a

azabost

11/15/2023, 1:35 PM
How can I access
::class
of a nullable reference? I can do it this way:
nullableVal?.let { it::class }
but I would like to know if there is a shorter version. It's a shame I can't just
nullableVal?::class
instead, like I can with
nullableVal?.javaClass
😅
p

PHondogo

11/15/2023, 1:50 PM
You could write extension property. Smth like this: val Any?.klass : KClass? get() = if (this == null) null else this::class
a

azabost

11/15/2023, 1:51 PM
Thanks for the tip. I was wondering if anything similar is already in the stdlib though 🙂
s

Shawn

11/15/2023, 4:55 PM
Technically, there's
?.javaClass?.kotlin
but that's definitely kinda goofy-looking
👍 1
e

ephemient

11/16/2023, 9:40 AM
2 Views