4

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?

3 Answers 3

13

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

oops, I've been awake too long. Thanks :)
Try (doc doc) to see whether it does. I have a hunch that it does though.
7

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

(with-out-str (doc f))

Comments

3

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.

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.