3

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.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.