Strict type/instance check in kotlin

I mean for eg :

open class A {}
class B : A() {}
val listOfABs = listOf(B(),B(),A(),A())
val list = listOfABs.filterIsInstance<A>() // It will return same list as listOfABs (listOf(B(),B(),A(),A())) because B is subclass and also instance of A

What i am looking for is list should have only listOf(A(),A())

What about creating such util by ourselves?

inline fun <reified R> Iterable<*>.filterIsInstanceStrict(): List<R> {
    @Suppress("UNCHECKED_CAST")
    return filter { it?.javaClass == R::class.java } as List<R>
}
3 Likes

i have already considered listOfABs.filter { it::class == A::class } but not sure if it is the right way of doing.
Please suggest