On Saturday, October 22, 2011 4:31:29 PM UTC-4, Luc wrote:
>
> Where's the contract breach here ?
>
Glad you asked. Consider the following clojure session (1.3), shortened for
your reading pleasure:
map-1 => {-1 :yo}
map-2 => {-1 :yo}
key-1 => -1
key-2 => -1
Just some simple maps and values, right?
(= map-1 map-2) => true
(= key-1 key-1 -1) => true
Yup, they're the same. But:
(map-1 key-1) => :yo
(map-2 key-1) => :yo
(map-1 key-2) => :yo
(map-2 key-2) => nil
Oops! Despite being "equal", the two maps behave differently. Why?
(class map-1) => clojure.lang.PersistentArrayMap
(class map-2) => clojure.lang.PersistentHashMap
(class key-1) => java.lang.Integer
(class key-2) => java.lang.Long
Unless I am mistaken, the difference between an ArrayMap and a HashMap is
supposed to be an implementation detail - an optimization. I'm sure that
they shouldn't have different semantics. But when hashCodes and equality do
not agree, this is the sort of thing that can happen.
Note that I'm not claiming to have any deep insights into what's broken and
what's not, either in Clojure or in Java. All I'm saying is that claiming
anything along the lines of "Clojure is not Java, so we can do whatever we
want - contracts do not apply" does not lead to sane map behavior. Those
contracts were created for a reason.
To be honest, I've sort-of lost the plot of how this is related to the
boxing-ints-as-Longs issue, but that's probably due to both my lack of
expertise in this area and to the generous glass of whiskey I had while
watching Megamind with my kids this afternoon. But I digress. The point I
think I was trying to back up is "if clojure changes equality semantics, it
should change hashcodes to match". That sounds right to me.
- Chris
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en