문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top