Skip to content

Commit dfbb852

Browse files
committed
made :default a keyword arg to defmulti, added support for docstring and metadata map, patch from mb
1 parent 8b09d47 commit dfbb852

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/clj/clojure/core.clj

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,14 +1008,40 @@
10081008

10091009
;;multimethods
10101010
(defmacro defmulti
1011-
"Creates a new multimethod with the associated dispatch function. If
1012-
default-dispatch-val is supplied it becomes the default dispatch
1013-
value of the multimethod, otherwise the default dispatch value
1014-
is :default."
1015-
([name dispatch-fn] `(defmulti ~name ~dispatch-fn :default))
1016-
([name dispatch-fn default-val]
1017-
`(def ~(with-meta name (assoc ^name :tag 'clojure.lang.MultiFn))
1018-
(new clojure.lang.MultiFn ~dispatch-fn ~default-val))))
1011+
"Creates a new multimethod with the associated dispatch function.
1012+
The docstring and attribute-map are optional.
1013+
1014+
Options are key-value pairs and may be one of:
1015+
:default the default dispatch value, defaults to :default"
1016+
{:arglists '([name docstring? attr-map? dispatch-fn & options])}
1017+
[mm-name & options]
1018+
(let [docstring (if (string? (first options))
1019+
(first options)
1020+
nil)
1021+
options (if (string? (first options))
1022+
(rest options)
1023+
options)
1024+
m (if (map? (first options))
1025+
(first options)
1026+
{})
1027+
options (if (map? (first options))
1028+
(rest options)
1029+
options)
1030+
dispatch-fn (first options)
1031+
options (rest options)
1032+
m (assoc m :tag 'clojure.lang.MultiFn)
1033+
m (if docstring
1034+
(assoc m :doc docstring)
1035+
m)
1036+
m (if (meta mm-name)
1037+
(conj (meta mm-name) m)
1038+
m)]
1039+
(when (= (count options) 1)
1040+
(throw (Exception. "The syntax for defmulti has changed. Example: (defmulti name dispatch-fn :default dispatch-value)")))
1041+
(let [options (apply hash-map options)
1042+
default (get options :default :default)]
1043+
`(def ~(with-meta mm-name m)
1044+
(new clojure.lang.MultiFn ~dispatch-fn ~default)))))
10191045

10201046
(defmacro defmethod
10211047
"Creates and installs a new method of multimethod associated with dispatch-value. "

0 commit comments

Comments
 (0)