https://kotlinlang.org logo
Title
h

HaSH

02/08/2019, 7:01 PM
The if would be here
d

dewildte

02/08/2019, 7:28 PM
Have you tried
if (!object.string.equals("string")) { code }
?
k

karelpeeters

02/08/2019, 7:28 PM
How do you know the strings actually match?
☝️ 1
h

HaSH

02/08/2019, 8:27 PM
@dewildte I hadn't, I just tried now and it appears .equals() is behaving as expected. I honestly thought == and .equals would work the same for String. Thanks for pointing me towards a solution.
k

karelpeeters

02/08/2019, 8:28 PM
They are the same!
(in Kotlin at least, not in Java)
h

HaSH

02/08/2019, 8:28 PM
@karelpeeters I put a few breakpoints in and manually evaluated
@karelpeeters haha. dun dun dun...plot thickens
(No idea why one works and not the other)
k

karelpeeters

02/08/2019, 8:29 PM
Can you take a look at the generated bytecode to see what
==
compiles down in your code?
How did you test equality with the debugger?
Are you sure you didn't accidentally do
===
?
d

dewildte

02/08/2019, 8:29 PM
From my (limited) understanding
!=
and
!string.equals(...)
are not the same.
h

HaSH

02/08/2019, 8:30 PM
@karelpeeters I'm not sure how to examine the bytecode, is it something I can easily see?
k

karelpeeters

02/08/2019, 8:30 PM
Tools > Kotlin > Show generated bytecode
(and then press decompile to get the Java version of the code)
d

dewildte

02/08/2019, 8:30 PM
CTRL+A "show kotlin byte code"
k

karelpeeters

02/08/2019, 8:31 PM
@dewildte Well except for some nullability stuff
!=
and
!.equals
are exactly the same.
d

dewildte

02/08/2019, 8:31 PM
Are they structural or referential?
h

HaSH

02/08/2019, 8:31 PM
To debug in AS(this is a android project) I put a break point in AFTER object is instantiated and also on the same line as the if statement. If you hover over the var in AS while at a stop point you can look at the underlying data
d

dewildte

02/08/2019, 8:31 PM
===
is referential is it not?
h

HaSH

02/08/2019, 8:32 PM
So I would know the value of object.string before the if code ran
k

karelpeeters

02/08/2019, 8:32 PM
Yes, :kotlin:
===
is reference equality, so :java:
==
, and :kotlin:
==
is structural so :java:
.equals
.
d

dewildte

02/08/2019, 8:33 PM
Java will give you a warning when trying to compare strings with
==
and
!=
. Warning says to use
.equals(...)
That's why I suggested using it.
h

HaSH

02/08/2019, 8:33 PM
That show bytecode feature is cool. but I don't know how to read bytecode lol
Give me a sec to look
k

karelpeeters

02/08/2019, 8:34 PM
That's what the compile button is for 😒imple_smile:
@dewildte But this is Kotlin!
d

dewildte

02/08/2019, 8:34 PM
Wibbly wobbly Kotlin javaey stuff.
lol
Anyway
.equals(...)
works for John.
I have contributed my piece. lol
h

HaSH

02/08/2019, 8:36 PM
Seems they call different things in bytecode?
d

dewildte

02/08/2019, 8:36 PM
AHA!
h

HaSH

02/08/2019, 8:36 PM
The .equals:
INVOKEVIRTUAL java/lang/String.equals (Ljava/lang/Object;)Z
d

dewildte

02/08/2019, 8:36 PM
The truth is revealed!
h

HaSH

02/08/2019, 8:36 PM
The ==: INVOKESTATIC kotlin/jvm/internal/Intrinsics.areEqual (Ljava/lang/Object;Ljava/lang/Object;)Z
Or am I reading things wrong haha
k

karelpeeters

02/08/2019, 8:37 PM
And you'll never guess what the source of that function is like...
h

HaSH

02/08/2019, 8:37 PM
lol
k

karelpeeters

02/08/2019, 8:37 PM
public static boolean areEqual(Object first, Object second) {
    return first == null ? second == null : first.equals(second);
}
h

HaSH

02/08/2019, 8:38 PM
haha
d

dewildte

02/08/2019, 8:38 PM
Wow
k

karelpeeters

02/08/2019, 8:38 PM
So there must be something else going on.
d

dewildte

02/08/2019, 8:38 PM
That is casting them as Object, but that should not matter.
I swear string equality is tricky.
h

HaSH

02/08/2019, 8:39 PM
I'm far from a "real" programmer, but nothing jumps out at me as to why this is happening.
k

karelpeeters

02/08/2019, 8:40 PM
Would it be possible to create a minimal example we can run?
d

dewildte

02/08/2019, 8:40 PM
Dude you just compiled byte code. How much more "real programmer" do you need?
h

HaSH

02/08/2019, 8:40 PM
A job
😉
d

dewildte

02/08/2019, 8:40 PM
Lol!!!!
h

HaSH

02/08/2019, 8:41 PM
Let me see how much code is related to this
d

dewildte

02/08/2019, 8:41 PM
I am laughing so hard right now
h

HaSH

02/08/2019, 8:41 PM
👍
I think I can get an example made up in a couple mins, will post if I can recreate
d

dewildte

02/08/2019, 8:45 PM
Cool
h

HaSH

02/08/2019, 8:59 PM
Yea, of course... I can't recreate in a separate project. Figures...
k

karelpeeters

02/08/2019, 9:00 PM
It could be a debugger bug as well.
h

HaSH

02/08/2019, 9:36 PM
Yea, I can't reproduce. Figures! haha. Thanks for the help!
👍 2