Hey guys,

I'm having some issues with defining types using deftype and would really appreciate some help.

Inspired by Mark Engelberg's PersistentPriorityMap http://github.com/clojure/clojure-contrib/blob/master/modules/priority-map/src/main/clojure/clojure/contrib/priority_map.clj, I decided to try out types and protocols with a MultiMap implementation

A MultiMap is just a map with more than one value associated with a key and I created a new type (PersistentMultiMap) and a new Protocol (MultiMap) with the following methods

(defprotocol MultiMap
  (seq-pairs [this])
  (assoc-swap [this key coll])
  (conj-swap [this key-coll])
  (dissoc-one [this key val]))

The problem is that the MultiMap protocol alone makes no semantic sense without functions like assoc and dissoc, so updating MultiMap

(defprotocol MultiMap
  (count [this])
  (assoc [this key val])
  (empty [this])
  (cons [this e])
  (equiv [this o])
  (containsKey [this key])
  (entryAt [this key])
  (seq [this])
  (without [this key])
  (seq-pairs [this])
  (conj-swap [this key-coll] [this key-coll & key-colls])
  (assoc-swap [this key coll] [this key coll & key-colls])
  (dissoc-one [this key val] [this key val & kvs]))

Great, now for the implementation.

My first attempt was a deftype (like the PersistentPriorityMap). The problem was that since PersistentMultiMap also implemented clojure.lang.IPersistentMap the methods clashed and the code would not compile. #gist http://gist.github.com/580454#file_gistfile1.clj

After implementing MultiMap with extend-type, the code compiled and everything worked fine #gist http://gist.github.com/580454#file_gistfile2.clj

I removed the clashing methods and redefined it inline and then the compilation went without a hitch #gist http://gist.github.com/580454#file_gistfile3.clj

Is there any way to define the MultiMap protocol inline with the clashing methods?

--
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
Note that posts from new members are moderated - please be patient with your 
first post.
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

Reply via email to