protocol extensions are applied on a per-namespace basis. What you describe shouldn't happen...it sounds to me that your tests for heap1.clj somehow knows about the heap2.clj, otherwise it wouldn't be possible to return an object from that namespace. In fact, if you do a

lein test someNS.heap1

the protocol extensions for heap2.lj shouldn't run, unless there is a dependency somewhere.

HTH,
Jim


On 26/09/13 13:10, Dustin Conrad wrote:
I am pretty new to Clojure, so what I am trying to do may make no sense. I am completely open to alternative ways to get similar functionality

I have a Heap protocol and two records that implement that protocol:

(defprotocol Heap
  (min [this]
  (insert [this x]))

In heap1.clj
(defrecord Heap1 [n]
  (min [this] (comment do stuff))
  (insert [this x] (comment insert stuff)))

In heap2.clj
(defrecord Heap2 [n]
  (min [this] (comment do some different stuff))
  (insert [this x] (comment insert stuff in a different way))

I found it useful to use extend-type to define this protocol on nil as well.

In heap1.clj
(extend-type nil
  Heap
  (min [this] (throw (Exception.))
  (insert [this x] (->Heap1 x)

This is all still working. In heap2.clj I would like to define another implementation of heap on nil, for when I require/use heap2.clj .
In heap2.clj
(extend-type nil
  Heap
  (min [this] (throw (Exception.))
  (insert [this x] (->Heap2 x)

Now, in the tests for heap1.clj where I am not requiring the heap2 namespace (or even know about it), my tests are failing because (insert nil 1) now returns a heap2 record. At this point I decided I obviously did not understand what extend-type actually does, and tried to do a bit of research, but I couldn't really find anything that went very in-depth.

If someone could provide an alternative to what I am trying to do, that would be great. Alternatively, it would be useful for someone to explain what extend-type does under the covers a bit.



--
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
--
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
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to