I have this simple example. Why does `a` compile f...
# getting-started
m
I have this simple example. Why does
a
compile fine, but
b
is a compilation error? I wouldn't expect
a
to compile but it does, but i'm also surprised it's not consistent between the two.
Copy code
data class ContactId(val value: UUID)

fun main(){
    val contactId = ContactId(UUID.randomUUID())
    val uuid = UUID.randomUUID()
    val a = contactId == uuid
    val b = contactId.toString() == uuid
}
y
https://kotlinlang.slack.com/archives/C09222272/p1761753181389429?thread_ts=1761748362.976979&cid=C09222272 TL;DR: custom
equals
is a thing, and it can do crazy stuff. Kotlin thus only warns if it knows that the equality absolutely can't happen
m
Not sure I like that, but I'll see what our linter can do. Thanks for the quick response!
k
A simple workaround in your case could be:
Copy code
@JvmInline
value class ContactId(val value: UUID)
which makes
a
a compilation failure as you wanted it to be. However, there may be some issues with some JSON libraries that don't recognise inline classes.
❤️ 1