I will not in any way claim to know how or why this works. I'm just
starting to use multimethods myself, but I'll give you my set up that
appears to be working at the moment.
I have a namespace:
(ns store.store)
(defmulti serialize method)
(defmulti slurp method)
in store.clj
I have 1 implementation:
(ns store.file-system
[:use [store.store]]
…)
(def base "")
(defmethod serialize :file-system [_ file-name contents]
(fs-utils/write-to contents (str base "/" file-name)))
(defmethod slurp :file-system [_ file-name]
(clojure.core/slurp (str base "/" file-name)))
I then use this from another namespace, requiring store.store and
store.store.file-system:
(ns library
[:require [wallpaper-manager-core.store.file-system :as
store-file-system]]
[:require [wallpaper-manager-core.store.store :as store]])
(binding [store-file-system/base (fs/home)]
(def library (ref (read-library) :validator library-validator)))
(defn serialize-library [library]
(store/serialize :file-system "library.clj" library))
And this all seems to work fine for me. Maybe someone else can explain
why it does for me and doesn't for you. Maybe it has something to do
with `use` vs. `require`?
> 1) Is it a bad idea to try to put the defmulti and defmethods into separate
> namespaces?
I don't personally see it as a bad idea, simply because I see defmulti
as a nice, clean way of defining an Interface with multiple
implementations to choose from. One of the ways that I'm using it it
to implement my html vs. json routes. I'm hoping that I can have a
route1.json namespace for all of the json requests and route1.html for
all the html requests. Based on my success with the store namespace I
don't foresee this being a problem, but maybe it will.
I heavily edited the code to remove uninteresting bits and I didn't
test it so be aware of that. :)
Hope that helps!
--
In Christ,
Timmy V.
http://blog.twonegatives.com/
http://five.sentenc.es/ -- Spend less time on mail
On Friday, March 2, 2012 7:03:10 AM UTC-6, tim.visher wrote:
I will not in any way claim to know how or why this works. I'm just
starting to use multimethods myself, but I'll give you my set up that
appears to be working at the moment.I have a namespace:
� � (ns store.store)
� � (defmulti serialize method)
� � (defmulti slurp method)
in store.clj
I have 1 implementation:
� � (ns store.file-system
� � � [:use [store.store]]
� � � �)� � (def base "")
� � (defmethod serialize :file-system [_ file-name contents]
� � � (fs-utils/write-to contents (str base "/" file-name)))� � (defmethod slurp :file-system [_ file-name]
� � � (clojure.core/slurp (str base "/" file-name)))
I then use this from another namespace, requiring store.store and
store.store.file-system:
� � (ns library
� � � [:require [wallpaper-manager-core.store.file-system :as
store-file-system]]
� � � [:require [wallpaper-manager-core.store.store :as store]])� � (binding [store-file-system/base (fs/home)]
� � � (def library (ref (read-library) :validator library-validator)))� � (defn serialize-library [library]
� � � (store/serialize :file-system "library.clj" library))
And this all seems to work fine for me. Maybe someone else can explain
why it does for me and doesn't for you. Maybe it has something to do
with `use` vs. `require`?
This does indeed work for me. What I was trying to do was avoid was having to do this part:
� �...� � ��[:require [wallpaper-manager-core.store.store :as store]])
As each time I add a defmethod implementation of my defmulti I'd have to add another require. But maybe that isn't such a bad thing so I'll go with this approach. I prefer it over having a "super parent (*)" namespace unless i needed that "super parent" in multiple places.
* by "super parent" I mean a namespace that is only used to include the namespaces that contain the defmulti and defmethods
Thanks,Cymen
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en