I recently asked about composite keys in maps in clojure : How can you implement Composite keys in clojure? …
The answer was that they work similar to java keys – if something overrides “equals”, then it can be used effectively as a key.
I was wondering : Are there macros that allow us to override “equals” for custom data structures ? For example, say I wanted to use a Map as a key, and define uniqueness as “if this map contains 2 or more elements in common with another map, they are equal”. How could I override the default behavior of a map ?
In java, I find this facility to be quite powerful when making high speed maps with thousands of beans as keys.
It’s not that clojure collections override equals; overriding equals happens almost everywhere you’re doing something interesting. Clojure types provide an implementation of equals that is intended to be consistent for the whole language (arguably in a way that Java’s equals was intended to be used). That means that things that are “equal” should be singular items in sets, and singular keys in maps, always, everywhere. The language design depends on that. Clojure 1.3 made some explicit non-backward compatible changes to get closer to that ideal.
Going against that expected behavior of equals is very likely going to cause trouble somewhere, somehow. And it’s not too difficult to use your own set-like composites when you really need them without forcing your will on core equals anyway.
That said, you can use many java interop functions and macros to pervert the equals system if you really want to. See http://clojure.org/datatypes as a starting point.