Docstring for custom definitions

In a project that I work on, we often define custom defsomething-style macros for different purposes to hide the template. One example is defhookthat that helps determine the hook handler for an event. Here's a simplified version (the actual version has more options and does some non-trivial things in defmethod, but doesn't matter for my question):

(defmulti handle-hook
  "This multimethod is called when an event was fired."
  (fn [event context] event))

(defmacro defhook
  "Define a hook for an event."
  [event docstring & more]
  `(let [body# (fn ~@more)]
     (defmethod handle-hook ~event [event# context#]
       (body# context#))))

(defhook "EntryDeleted"
  "Hook called on entry deletion."
  [context]
  (log-deletion (:EntryID context)))

, defmethod docstring, "EntryDeleted" REPL . : defhook defhandler, API, ( ).

, : " docstring defmethod"?.

" / defsomething?"

, ! Marginalia, Codox Autodoc, , - .

+4
1

/ defsomething?

docstrings vars, defsomething, def, . defn, def. defsomething docstring var.

docstring defmethod?

- defmethod var; Java- Java. , defmulti var. docstring . ,

(defn append-hook-doc! [event docstring]
  (let [hook-doc (str event " - " docstring)]
    (alter-meta! #'handle-hook
                 (fn [m]
                   (update-in m [:doc] #(str % "\n\t" hook-doc))))))
...
(doc handle-hook)
-------------------------
user/handle-hook
  This multimethod is called when an event was fired.
      EntryDeleted - Hook called on entry deletion.

!, : , , #'handle-hook docstring. , #'handle-hook, , . - , . (defmulti handle-hook ... , ( ).

+1

Source: https://habr.com/ru/post/1540863/


All Articles