https://kotlinlang.org logo
#javascript
Title
# javascript
e

Edoardo Luppi

12/04/2023, 11:42 AM
The
KClass#hashCode
method uses the class simple name
Copy code
// TODO: use FQN
override fun hashCode(): Int = simpleName?.hashCode() ?: 0
I suppose it's better to avoid using it as of now, as we might have collisions
a

Artem Kobzar

12/04/2023, 12:09 PM
What could we use in this case?
e

Edoardo Luppi

12/04/2023, 12:10 PM
Seems like the TODO suggests the fully qualified name, but personally I have no idea if it's enough or not
a

Artem Kobzar

12/04/2023, 12:15 PM
Yes, but as far as I remember, we don't save this information for runtime (because fqn are long, and they make the bundle heavier)
e

Edoardo Luppi

12/04/2023, 12:15 PM
Yeah, the qualified name property is marked with a
TODO()
For the couple of places I need this, I have manually typed the FQN. No big deal
Probably some kind of compile-time warning could be emitted when using
hashCode
tho, just so the consumer is aware of the potential collision issue
j

jw

12/04/2023, 7:52 PM
But that's always a possibility with hashCode anyway
e

Edoardo Luppi

12/04/2023, 8:01 PM
But in this case the possibility is quite high, that's why I mentioned a warning
j

jw

12/04/2023, 8:03 PM
It is, but hashCode is typically used in a polymorphic context where you've completely lost the original type. Or the hashCode usage is abstracted away such as in a map or set.
2 Views