https://kotlinlang.org logo
Title
l

LeoColman

02/14/2019, 3:34 PM
I ran into an issue sooner today when I serialized a data class that contained an enum, and the hashcode was always different
i

ilya.gorbunov

02/14/2019, 5:51 PM
Could you provide a sample which shows that behavior?
l

LeoColman

02/14/2019, 9:32 PM
I'll try to make one. Basically, what I was doing is something like this:
var code = 6
code = 31 * code + javaLocalDate.hashCode()
code = 31 * code + MyEnum.Foo.hashCode()   // I didn't overwrite this enum
return code
And I noticed that this would always generate a new hashcode, for the exact same values
I then changed the third line to
code = 31 * code + MyEnum.Foo.toString().hashCode()
And it worked fine from then on
I'll get a concrete example.
i

ilya.gorbunov

02/15/2019, 3:35 AM
Or do you mean that you're serializing an enum and after deserializing it in a different process it gets different
hashCode
? In that case it is normal, nothing in hashCode spec implies it should preserve its value in across processes.
l

LeoColman

02/15/2019, 12:25 PM
I think that's closer to what I meant.
That's what doesn't make sense to me
Why chossing this for the default Enum implementation? I get it that it isn't against the specification (although it does seem counter-intuitive), but it makes sense to return the same value always!