How can I do something like this? ```fun isClass(t...
# getting-started
d
How can I do something like this?
Copy code
fun isClass(theObject : Any, theClass : KClass) : Boolean {
    if (theObject is theClass) {
        return true
    }
    return false
}
I would like to use it in this way:
Copy code
val verification = isClass(myObject, Person::class)
a
Copy code
val verification = myObject is Person
r
you should use it directly like that ↑ but if you need to group it with some other validation or smth you can use reified inline functions
Copy code
inline fun <reified T> isClass(theObject: Any): Boolean {
    // some other logic
    return theObject is T
}


val obj: Any = "Some string"
val verification = isClass<String>(obj)
DO NOT use it if you just want to do the simple check, as the is operator is way more readable. So use this only if you need to do some additional logic, or call some additional functions...
d
I need to pass the
Class::class
to the function, because there is some processing I need to do with this information
r
in the reified function, you can use
T::class
d
I found out I can pass it like
KClass<*>
Copy code
fun getFromAppState(theClass : KClass<*>) : Any? {
}
and then:
Copy code
if (theClass.isInstance(obj)) {
    ...
}
k
theClass.isInstance?
d
yes, that was what I was looking for
so, in summary, the original function would be:
Copy code
fun isClass(theObject : Any, theClass : KClass<*>) : Boolean {
    if (theClass.isInstance(theObject)) {
        return true
    }
    return false
}
👍 1
r
you can do the same thing with the reified function 😛
Copy code
fun someOtherLogic(theClass: KClass<*>): Unit = TODO()

inline fun <reified T> isClass(theObject: Any): Boolean {
    someOtherLogic(T::class)
    return theObject is T
}

val obj: Any = "Some string"
val verification = isClass<String>(obj)
like this - the call, after the function is inlined would look like:
Copy code
val obj: Any = "Some string"
someOtherLogic(String::class)
val verification = obj is String
d
thanks! is there any particular advantage in using a reified function, or is it just a matter of taste?
r
advantage is mostly in api while calling it a) you don't have to do
::class
while calling it everywhere b) if the type can be inferred, you don't need to specify it at all (which is probably not case here tho)
eg, I'm sure you ran into some reflection functions that have signatures like
Copy code
fun <T> someWork(theObject: T, theClass: KClass<T>): Unit = TODO()
when you calling them you need to repeat yourself:
Copy code
val x: String = "abc"
someWork(x, String::class)
if you had an reified function to accompany it, (which thankfully most Kotlin reflection libraries do)
Copy code
inline fun <reified T> someWork(theObject: T): Unit {
    someWork(theObject, T::class)
}
you can simply call it:
Copy code
val x: String = "abc"
someWork(x)
(since it will infer that to
someWork<String>(x)
automatically)
d
yes, it makes sense However in some use cases you don’t have an instance to pass, you just want to pass the class. In such case
KClass<*>
is very handy.
I just found out
KClass<T>
exists too, that is very handy, and yes, it requires “reified”