https://kotlinlang.org logo
#announcements
Title
# announcements
b

bbaldino

07/23/2019, 9:58 PM
what's the best way to do an
is
style check when the type is erased (and i can't use
reified
)? is it to take in a
KClass
as an arg and do something like:
Copy code
fun<T> matches(value: KClass<V>): Boolean = Class::class.java.kotlin == value
and call it via
matches(MyClass::class.java.kotlin)
?
d

Dominaezzz

07/23/2019, 10:00 PM
Do you really really need the
is
check to happen?
b

bbaldino

07/23/2019, 10:00 PM
i do, it's for a predicate that's based on an instance check
d

Dominaezzz

07/23/2019, 10:01 PM
You're solution is close but it doesn't handle subclasses.
I'm not a pro at reflection-y stuff either. 😅 I tend to avoid it.
b

bbaldino

07/23/2019, 10:02 PM
i had used reified with inlining--which does work, but unfortunately ran into a bug similar to this one https://youtrack.jetbrains.com/issue/KT-32749
yeah good catch on the subclass thing, i would want those to match
k

karelpeeters

07/23/2019, 10:36 PM
Copy code
fun matches(value: Any, type: KClass<*>): Boolean =
    type.java.isAssignableFrom(value::class.java)
k

kralli

07/24/2019, 6:13 AM
You can use `KClass<*>`’s
isInstance(Any?)
which is basically a dynamic
is
.
b

bbaldino

07/24/2019, 4:34 PM
thanks!