Greetings,

Suppose that I create a function that is designed to receive a map as
a parameter:

(defn add-ten [m]
  (+ 10 (:amount m)))

but then later I want to make this function part of a protocol

(defprotocol AddTen
  (add-ten [this]))

but I still want it to work when someone passes a plain map to add-
ten. The only thing that I can think of would be the following:

(extend-protocol AddTen
  clojure.lang.IPersistentMap
  (add-ten [this] (+ 10 (:amount this))))

It feels wrong to refer to clojure.lang.IPersistentMap as if I am
relying on an implementation detail. Is there a better way to do
this?

I'm in the middle of writing blog post about protocols (in order to
improve my own understanding of them) where I try to illustrate the
point that you can code against maps then switch to protocols and
records without changing the caller's contract.

Thanks for your help,
Brenton

-- 
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