Fork me on GitHub
Alex Miller (Clojure team)04:07:04

FYI, I’ll be offline next week. Don’t break anything while I’m gone!

🙊 12
🙌 4
😬 8
👋 8

I’m sorry if this is written down somewhere, but i’ve not been able to find anything. I’m trying to familiarize myself with the compiler, and I’m seeing several classes have hashCode and hasheq fns. What’s the difference between these two? Thanks! (If anyone got some good links for understanding the clojure source code better, I’m very interested).


hashCode is normal java hashCode semantics, hasheq is clojure's hash where '(1 2 3) and [1 2 3] have the same hash


Hmm, ok. So hashCode is only used when storing Clojure data in Java collections? Any reason why hashCode doesn’t just use hasheq? Different hash algorithms?


Clojure's hashCode return values are consistent with Java's .equals method for Clojure collections that can be .equals to corresponding Java collections.


So Clojure PersistentList .hashCode returns the same hash values that a Java ArrayList instance with the same sequence of .equals elements would return.


Until Clojure 1.6.0, Clojure's hasheq on lists, vectors, maps, and sets were the same as hashCode. Shortly before Clojure 1.6.0 was released, it was discovered that this choice of hasheq same as hashCode was poor for some relatively common Clojure program use cases where lists/vectors/sets/maps were used as set elements, or keys in maps, and had many hash collisions.


At that time, hashed was modified to be different than hasheq for those collections, but hashCode was left consistent with Java equals for when such collections were used in Java libs.


Maybe more detail than you want on that change is on this wiki page:


Great answer, thank you!