Why is `MyId(1) === MyId(1)` not `true`? Why does ...
# getting-started
s
Why is
MyId(1) === MyId(1)
not
true
? Why does it still see two object references? There can't be two "instances" of a primitive long on the JVM. (Also
1L === 1L
is true.)
Copy code
@JvmInline
value class MyId(
    val value: Long
)
1
m
Because inline classes may be represented both as the underlying value and as a wrapper, referential equality is pointless for them and is therefore prohibited.
https://kotlinlang.org/docs/inline-classes.html
thank you color 1
e
Also
1L === 1L
is true.
only somewhat accidentally, on most JVMs
"128".toLongOrNull() !== "128".toLongOrNull()
and there's ways to bypass the small number cache even for smaller constants
thank you color 1
c
Which version of Kotlin are you using? Referential equality on value classes has been forbidden for a few versions (playground). Also,
1L === 1L
is deprecated (CTRL ALT H to see warnings) and will be forbidden in the future (for the reasons explained by @ephemient).
s
1.9.23
In playground I get this error message too, but in IDEA I just receive "false"
c
Maybe you have an old IDEA version?
s
2024.1
But doesn't matter. Eventually it will show the same error message and my question is answered. 🙂
1