yazz.com
yazz.com

Reputation: 58826

How to show documentation in Clojure

I do the following :

(defn ss [] "kjhhj")
(doc ss)

but get "nil" returned. Why is this?

Update:

If I do :

(defn tt "kjhhj" [] 1)
(str (doc tt) )

as shown I get back an empty string... Does the "doc" output go to out or something?

Upvotes: 4

Views: 1636

Answers (3)

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44162

To capture the output of something that prints to *out*, use

(with-out-str (doc f))

Upvotes: 7

abedra
abedra

Reputation: 384

doc does not return anything, it just prints to the screen. Take a look at the source

Clojure 1.3.0-master-SNAPSHOT
user=> (source doc)
(defmacro doc
  "Prints documentation for a var or special form given its name"
  {:added "1.0"}
  [name]
    (if-let [special-name ('{& fn catch try finally try} name)]
      (#'print-doc (#'special-doc special-name))
      (cond
        (special-doc-map name) `(#'print-doc (#'special-doc '~name))
        (resolve name) `(#'print-doc (meta (var ~name)))
        (find-ns name) `(#'print-doc (namespace-doc (find-ns '~name))))))

Calling (str) on (doc) will always return nil.

Upvotes: 3

dfan
dfan

Reputation: 5824

The docstring comes before the arguments to the function. You have defined a function with no docstring that returns a string.

user> (defn ss [] "kjhhj")
#'user/ss
user> (ss)
"kjhhj"
user> (doc ss)
-------------------------
user/ss
([])
  nil
nil

user> (defn tt "kjhhj" [])
#'user/tt
user> (tt)
nil
user> (doc tt)
-------------------------
user/tt
([])
  kjhhj
nil
user> 

Upvotes: 13

Related Questions