https://kotlinlang.org logo
Title
s

StavFX

01/17/2018, 1:57 AM
You could just simplify this all to
it == obj
- the only caveat is that if
it
and
obj
are both
null
- you'll get
true
. If that's not desired, then this would yield the same result as the original code
it != null && it == obj
s

spragg

01/17/2018, 2:09 AM
This assumes that
it
and
obj
are of the same type. Using equals allows for a custom comparison function. In this case I have a custom data class which I want to support a comparison to an ID of type Long.
s

Shawn

01/17/2018, 2:20 AM
sure, but doesn’t
==
translate to a
.equals()
call in Kotlin?
or am I missing an edge case or something else unintuitive?
s

StavFX

01/17/2018, 4:20 AM
Yes @Shawn you are correct, in kotlin
a == b
and
a.equals(b)
are identical. @spragg if you want the java "==" you'll have to use === in kotlin.
s

spragg

01/17/2018, 4:29 AM
Nice, yeah it does. @StavFX I only had that second part in to support matching a
null
, and since that is taken care of by what == is translated to. I can do what I need by just having
a == b
👍 1