On Mon, Dec 8, 2008 at 5:35 PM, Chouser <[EMAIL PROTECTED]> wrote: > > On Mon, Dec 8, 2008 at 3:40 PM, J. McConnell <[EMAIL PROTECTED]> wrote: > > > This is a breaking change for some (granted rather unsual) cases: > > (defmulti foo {:a 1 :b 2}) > (defmethod foo 1 [_] "got a1") > (defmethod foo 2 [_] "got b2")
Or, worse yet: (defmulti foo {:a {:b "B"}} {:c "C"}) Which would become ambiguous after this patch. Good catch, Chouser. > I'm not offering an opinion here on whether or not it's a good patch, > just wanted to point out it changes currently defined behavior. Thanks for pointing that out. I did this mostly as a learning experience, because it seemed like something that would be useful. However, it's not something I have a need for at the moment, so I've mostly gotten out of it what I was looking for :) Before giving up on it, one approach that might work for now would be to allow a doc-string, but not the attr-map. Since a string isn't a valid parameter for the dispatch function, there would be no possibility for ambiguity and I believe it is completely backwards compatible. I've attached a patch that implements this. Sample usage: 1:7 user=> (defmulti #^{:arglists '([n])} fib "docs for fib" int) #'user/fib 1:8 user=> (doc fib) ------------------------- user/fib ([n]) docs for fib nil Thoughts welcome. I'll be happy to implement any suggested changes. If, on the other hand, there is no further interest in this, I'll let it go since I don't have a need for it at the moment, anyhow. However, it does seem like it would be nice if defmulti had similar doc support to defn and defmacro. Regards, - J. --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---
Index: src/clj/clojure/core.clj =================================================================== --- src/clj/clojure/core.clj (revision 1147) +++ src/clj/clojure/core.clj (working copy) @@ -991,11 +991,19 @@ default-dispatch-val is supplied it becomes the default dispatch value of the multimethod, otherwise the default dispatch value is :default." - ([name dispatch-fn] `(defmulti ~name ~dispatch-fn :default)) - ([name dispatch-fn default-val] - `(def ~(with-meta name (assoc ^name :tag 'clojure.lang.MultiFn)) - (new clojure.lang.MultiFn ~dispatch-fn ~default-val)))) + ([name & fdecl] + (let [docs (if (string? (first fdecl)) {:doc (first fdecl)}) + dispatch-fn (if docs (second fdecl) (first fdecl)) + default-val (or (if docs (second (rest fdecl)) (second fdecl)) + :default)] + `(def ~(with-meta name (conj (assoc ^name :tag 'clojure.lang.MultiFn) docs)) + (new clojure.lang.MultiFn ~dispatch-fn ~default-val))))) +(.setMeta (var defmulti) + (conj (meta (var defmulti)) + {:arglists '([name doc-string? dispatch-fn] + [name doc-string? dispatch-fn default-val])})) + (defmacro defmethod "Creates and installs a new method of multimethod associated with dispatch-value. " [multifn dispatch-val & fn-tail]