开发者

How to show documentation in Clojure

开发者 https://www.devze.com 2023-02-24 08:21 出处:网络
I do the following : (defn ss [] \"kjhhj\") (doc ss) but get \"nil\" returned. Why is this? Update: If I do :

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)开发者_Python百科 )

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


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> 


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

(with-out-str (doc f))


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消