https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
m

Matthew Murray

07/26/2022, 7:45 PM
I’m seeing an issue in Kotlin/Native where === is returning false for two variables that both point to same ObjC object (== works fine). Is this a known issue that === doesn’t compare pointers as expected when comparing ObjC objects? Or is my understanding of === equality missing something?
r

rnett

07/26/2022, 8:17 PM
To hazard a guess, is the ObjC object wrapped in a Kotlin object, even internally? Because then
===
would be comparing the two Kotlin boxes by reference, and they wouldn't be equal
m

Matthew Murray

07/26/2022, 8:18 PM
ahh, that could be it. Not ideal functionality but would make sense.
l

Landry Norris

07/27/2022, 6:04 PM
Do you have some minimal code that reproduces this?
m

Matthew Murray

07/28/2022, 9:05 AM
@Landry Norris Sorry for delay. Here’s a quick example distilled to its simplest form that is failing for me.
Copy code
fun compare(item : Any, withItem : Any) : Boolean {
  return item === withItem
}
And then from ObjC I can call it like this
Copy code
BOOL equality = [[Utils shared] compareItem:view withItem:view];
And it will return false - where
view
is a UIView
l

Landry Norris

07/28/2022, 1:35 PM
I would imagine the Kotlin compiler wraps the Obj-C objects in a KObjHeader under the hood, and === is comparing that. If view were passed straight to Kotlin, I'd think === would work
m

Matthew Murray

07/28/2022, 7:25 PM
that seems to be the case. Definitely something to be aware of that could catch you off-guard. Seems like something the Kotlin compiler should account for when compiling for objc targets.
l

Landry Norris

07/28/2022, 7:26 PM
Are you able to use == for this?
m

Matthew Murray

07/28/2022, 7:27 PM
ya == works for me. I can’t think of an edge case where it wouldn’t, but === just seemed more precise for what I am trying to do
l

Landry Norris

07/28/2022, 7:28 PM
Yeah. It may just be something to note in some documentation. It makes sense that they wouldn’t add an exception to === for this case.
2 Views