what's the best way to do an `is` style check when...
# announcements
b
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
Do you really really need the
is
check to happen?
b
i do, it's for a predicate that's based on an instance check
d
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
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
Copy code
fun matches(value: Any, type: KClass<*>): Boolean =
    type.java.isAssignableFrom(value::class.java)
k
You can use `KClass<*>`’s
isInstance(Any?)
which is basically a dynamic
is
.
b
thanks!