Null-safe ::class


#1

Is there or can there be a syntax for a null-safe ::class access?

i.e. I can do

myNullableVar?.foo()

but not(?) something like

myNullableVar?::class

As far as I know, I would need to do something like

if (myNullableVar == null) null else myNullableVar::class

#2

or perhaps

val clazz : Class<T>? = myNullableVar?.let { it::class }


#3

I have tried the follwing code

fun main(args: Array<String>) {
    printClass("Helllo")
    printClass(1)
}


fun printClass(param: Any?) {
    val clazz: KClass<out Any>? = param?.let { it::class }
    if (clazz != null) {
        println(clazz.simpleName)
    }
}

Output:
String
Int

Process finished with exit code 0