I'm trying to write an extension method that check...
# announcements
a
I'm trying to write an extension method that checks if something is null, or if it equals a certain value. I wrote it like this:
Copy code
fun <T : Any?> T.isNullOrEqualTo(t : T): Boolean {
    return this == null || this == t
}
However it compiles totally fine if they're different types (even though it returns false). Like
3.equals("Pickles")
compiles, even if it's not valid. Is there any way to enforce that the two types are the same, without writing a bunch of type safe versions of this?