I've already posted here [1] and on the issue board [2] about hashing. In particular, .hashCode for seqs/colls break the Java contract that whenever (.equals x y), (= (.hashCode x) (.hashCode y)). (let x = [1] and y = (seq [1])). As I've mentioned earlier, I hope that eventually .hashCode and .equals will be brought into line. Anyway, I won't rehash (ha ha) this issue further here.
Anyway, I've also since realized that .equals and = are not the same thing, in particular with respect to numbers. So, it might be nice to have a different hash function (i.e., Clojure's "hash"), that matches the behavior of =, and allow the user to specify which hash function they'd like to use when creating hash-sets and hash-maps. (If you know Common Lisp, this idea should be very familiar to you). I think I've already seen at least one instance on the group of someone being thrown off by the fact that two numbers are Clojure = but treated as distinct keys in hash-maps (since they are not .equals to each- other). Actually, I'd be happy with a single type of hashing compatible with = rather than .equals if the idea of multiple flavors is not popular (as I imagine it probably won't be). And, lest you think that confusing = and .equals is just a noobie mistake, let me point out what seems to be a bug in PersistentHashMap.java I just found based on this same confusion. In particular, Objects are located in the map using Clojure's hash function, which is equivalent to .hashCode (and 0 for nil). But, the equality check done on line 561 is done using Util.equal, which is equivalent to Clojure =. Now, suppose that x is a key in the map and we're doing a lookup for y, where (= x y) but (not (.equals x y)). (i.e., x is (Integer. "12") and y is (Long. "12").). Then, the lookup will succeed iff there is a hash collision between x and y, which is presumably not desired behavior. (In fact, I think this will always happen for = Integers and Longs due to the way their hashCodes are defined, but for, e.g., Doubles and Integers it may only happen at a single number (if at all).) -Jason [1] http://groups.google.com/group/clojure/browse_thread/thread/5d11bc0da25a7ecc/06d6cd888a516119?hl=en&lnk=gst&q=.hashcode#06d6cd888a516119 [2] http://code.google.com/p/clojure/issues/detail?id=37&colspec=ID%20Type%20Status%20Priority%20Reporter%20Owner%20Summary --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---