https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Daniele B

03/22/2021, 10:33 AM
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

acoconut

03/22/2021, 10:55 AM
Copy code
val verification = myObject is Person
r

Roukanken

03/22/2021, 11:10 AM
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

Daniele B

03/22/2021, 11:47 AM
I need to pass the
Class::class
to the function, because there is some processing I need to do with this information
r

Roukanken

03/22/2021, 11:48 AM
in the reified function, you can use
T::class
d

Daniele B

03/22/2021, 11:49 AM
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

kqr

03/22/2021, 11:49 AM
theClass.isInstance?
d

Daniele B

03/22/2021, 11:49 AM
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

Roukanken

03/22/2021, 11:53 AM
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

Daniele B

03/22/2021, 11:56 AM
thanks! is there any particular advantage in using a reified function, or is it just a matter of taste?
r

Roukanken

03/22/2021, 12:00 PM
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

Daniele B

03/22/2021, 1:54 PM
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”
3 Views