I'm trying to compare nullable classes. Has anyone...
# announcements
c
I'm trying to compare nullable classes. Has anyone been able to do this? E.g.
Copy code
fun isObjectOfSameType(first: Comparable?, second: Comparable?) : Boolean {
            return first::class.java.isAssignableFrom(second)
        }
however
javaClass
or
::class.java
can't be called on
Nullable
types
a
You will need to add null checks then
Copy code
fun isObjectOfSameType(first: Comparable<*>?, second: Comparable<*>?): Boolean {
        if (first == null && second == null) {
            return true
        }
        if (first == null || second == null) {
            return false
        }
        return first::class.java.isAssignableFrom(second::class.java)
    }
🙏 1
c
Late to the party but thanks, I ended up doing that in many places, forgot to come back and now I see threads.