|
1008 | 1008 |
|
1009 | 1009 | ;;multimethods |
1010 | 1010 | (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))))) |
1019 | 1045 |
|
1020 | 1046 | (defmacro defmethod |
1021 | 1047 | "Creates and installs a new method of multimethod associated with dispatch-value. " |
|
0 commit comments