note, I didn't test any of these, but they should work (possibly with
a tweak or 2)

There's quite a few ways to do this, here's one.

(defmulti add-item (fn [& args] (condp count args 0 :none 1 (class
(first args)) :default))
(defmethod add-item :none (add-item nil nil))
(defmethod add-item Integer [id] (add-item id nil))
(defmethod add-item Map [props] (add-item nil props))
(defmethod add-item :default [id props] (  ; call some Java method )

Here's another

(defmulti add-item (fn [& args] (map class args)
(defmethod add-item [] (add-item nil nil))
(defmethod add-item [Integer] [id] (add-item id nil))
(defmethod add-item [Map] [props] (add-item nil props))
(defmethod add-item :default [id props] (  ; call some Java method )

Cheers, Jay

On Sun, Apr 15, 2012 at 5:33 PM, James Thornton
<james.thorn...@gmail.com> wrote:
> How do you use defmulti to create a function with a variable number of args?
>
> For example, add-item is wrapping a Java method that can take a variable
> number of args, and so here I am trying to make add-item take zero, one, or
> two args. Notice there are two singe-arg funcs -- each taking a different
> type...
>
> (defmulti add-item class)
> (defmethod add-item [] (add-item nil nil))
> (defmethod add-item Integer [id] (add-item id nil))
> (defmethod add-item Map [props] (add-item nil props))
> (defmethod add-item :default [id props] (  ; call some Java method )
>
> Unless I am overlooking something, I don't see anything on the Multimethods
> page (http://clojure.org/multimethods) about defmulti or defmethod taking
> a variable number of arguments -- they all take the same number of arguments
> of different types.
>
> - James
>
> --
> 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 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