Commenting, i.e. adding docstrings to multimethods in Clojure
Code
;; commeting_multimethods.clj
(defn arg-type [arg]
(cond
(string? arg) :string
(number? arg) :number
:else :other))
(defn dispatcher [args]
(map arg-type args))
(defmulti add
"Adds two strings or nubers.
Usage
----------
```clojure
(add 1 2)
(add \"a\" \"b\")
```
"
(fn [& args] (dispatcher args)))
(defmethod add '(:number :number) [a b]
(+ a b))
(defmethod add '(:string :string) [a b]
(str a b))
(doc add)
(add 1 2)
(add "a" "b")
The workaround I did before, which is not needed:
;; multimethod_commenting_work_around.clj
(defn arg-type [arg]
(cond
(string? arg) :string
(number? arg) :number
:else :other))
(defn dispatcher [args]
(map arg-type args))
(defmulti multi-add (fn [& args] (dispatcher args)))
(defmethod multi-add '(:number :number) [a b]
(+ a b))
(defmethod multi-add '(:string :string) [a b]
(str a b))
(defn add
"Adds two strings or nubers.
Usage
----------
```clojure
(add 1 2)
(add \"a\" \"b\")
```
"
[a b]
(multi-add a b))
(doc add)
(add 1 2)
(add "a" "b")
Notes