2

这按预期工作:

java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (with-bindings {#'clojure.repl/print-doc str} (eval '(clojure.repl/doc println))))"

输出:

"{:ns #<Namespace clojure.core>, :name println, :arglists ([& more]), :added \"1.0\", :static true, :doc \"Same as print followed by (newline)\", :line 3325, :file \"clojure/core.clj\"}"

但是这在 REPL 中不起作用:

java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (clojure.main/repl :init (fn [] {#'clojure.repl/print-doc str}))))"

输出(doc println)

user=> (doc println)
-------------------------
clojure.core/println
([& more])
  Same as print followed by (newline)
nil
user=>

我不知道我做错了什么。

4

1 回答 1

2

深入逆时针和nrepl代码后找到答案:

java -jar clojure-1.4.0.jar -e "(do (require 'clojure.repl) (.setDynamic #'clojure.repl/print-doc) (with-bindings {#'clojure.repl/print-doc str} (clojure.main/repl)))))"

输出与上面相同:

"{:ns #<Namespace clojure.core>, :name println, :arglists ([& more]), :added \"1.0\", :static true, :doc \"Same as print followed by (newline)\", :line 3325, :file \"clojure/core.clj\"}"

诀窍是with-bindings在调用之前使用repl

(with-bindings {#'clojure.repl/print-doc str}
  (repl))
于 2012-10-01T10:35:07.597 回答