Question

I want to be able to programatically define a docstring for one of my functions in clojure.

For example, I want to be able to do something like this:

(defn my-function
    (str "Here are some numbers " (range 10))
    []
    (println "This function does nothing right now."))

However, when I attempt to do this, I get "Parameter decleration str should be a vector". Is this just not possible in clojure or is there some sneaky way to do this? It would be useful to me to programatically generate parts of the docstring.

Was it helpful?

Solution

Yep, it's definitely possible. The thing you're running into here is that defn (or rather, the def special form that it expands to) attaches a docstring to the symbol iff the argument in second place is a string.

You can circumvent this by setting the :doc metadata yourself.

(defn ^{:doc (apply str "Here are some numbers " (range 10))} my-function
   []
   (println "This function does nothing right now."))

Or potentially by writing your own macro - but I think the above is the most straightforward way of doing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top