Here is another question on when you would use a multi-method.  I want the
mv function to accept a java.io.File or a String for either of the two
parameters and here are the options I came up with:

-----(defmulti mv (fn [from to] [(class from) (class to)]))
(defmethod mv [String String] [from to] (println "Both strings"))
(defmethod mv [File File] [from to] (println "Both files"))
(defmethod mv [File String] [from to] (println "File String"))
(defmethod mv [String File] [from to] (println "String File"))

(defn mv2 [from to]
  (let [f (if (= (class from) File) from (File. from))
        t (if (= (class to) File) from (File. to))]
    (println "transformed to File")))
-----

While I find that multi-methods allowing me to do the above exceedingly
cool, would it be more idomatic to just use the let and create a File if
passed a String.  Am I doing "too much work" for something relatively
simple.

The multi-method does have better (IMHO) errors when passed (mv ["foo"]
"bar"):
-----
No method in multimethod 'mv' for dispatch value:
[clojure.lang.LazilyPersistentVector java.lang.String]
  [Thrown class java.lang.IllegalArgumentException]
-----

This would tell me that I could write another multi-method that could move
multiple files from different locations passed in as a sequence to one
destination... which I didn't think I needed but could be pretty handy now
that I think on it.

--Robert

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