How can I get the `kClass` instance for the type o...
# getting-started
c
How can I get the
kClass
instance for the type of a nullable variable without using reified generics?
Copy code
fun <T : Any?> classOf(value: T): KClass<T & Any> = 
    TODO("what to put here??")
such that:
Copy code
val value: Int? = 5
returns
Int::class
? I can't use
value::class
because that's forbidden for nullable values
g
Formally, I don't understand what do you expect when
null
is provided. I mean, what is the `null`'s class?
null
has type
Nothing?
but it's not a class.
d
What's the aversion to using a reified type? Are you trying to get the specific subclass if it isn't a null?
Copy code
inline fun <reified T : Any> classOf(value: T?): KClass<out T> = value?.let { it::class } ?: T::class
c
@Gleb Minaev you're right. That makes sense.