pro*_*eek 6 emacs clojure leiningen
我有这个Clojure代码启动并执行一个函数.
(import [java.lang Thread])
(defn with-new-thread [f]
(.start (Thread. f)))
(with-new-thread (fn [] (print "hi")))
Run Code Online (Sandbox Code Playgroud)
但是,当我在emacs中以slime-repl模式运行它时(执行时cider-jack-in,没有打印出来,但是nil返回了.
有了lein real,我得到了预期的输出.
user=> (import [java.lang Thread])
java.lang.Thread
user=> (defn with-new-thread [f] (.start (Thread. f)))
#'user/with-new-thread
user=> (with-new-thread (fn [] (print "hello\n")))
hello
nil
Run Code Online (Sandbox Code Playgroud)
可能有什么问题?
发生这种情况是因为CIDER/Emacs中的主线程将REPL输出缓冲区绑定到*out*动态var.这就是为什么你可以在emacs中看到的东西.
但是,当您启动新线程时,该绑定不存在.修复很简单 - 首先创建一个捕获当前绑定的函数:
(def repl-out *out*)
(defn prn-to-repl [& args]
(binding [*out* repl-out]
(apply prn args)))
Run Code Online (Sandbox Code Playgroud)
现在,只要您想从不同的线程打印,请使用:
(prn-to-repl "hello")
Run Code Online (Sandbox Code Playgroud)
就是这样.希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |