gpt4 book ai didi

emacs - Clojure 的线程在 Emacs clojure-repl 模式下不显示结果

转载 作者:行者123 更新时间:2023-12-02 19:48:12 25 4
gpt4 key购买 nike

我有这个启动并执行函数的 Clojure 代码。

(import [java.lang Thread])
(defn with-new-thread [f]
(.start (Thread. f)))
(with-new-thread (fn [] (print "hi")))

但是,当我在 emacs 中以 slime-repl 模式运行它时(使用 cider-jack-in 执行),没有打印任何内容,但返回了 nil。

enter image description here

使用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

可能出了什么问题?

最佳答案

发生这种情况是因为 CIDER/Emacs 中的主线程将 REPL 输出缓冲区绑定(bind)到 *out* 动态变量。这就是为什么你可以在 emacs 中看到东西。

但是,当您启动一个新线程时,该绑定(bind)不存在。修复非常简单 - 首先创建一个捕获当前绑定(bind)的函数:

(def  repl-out *out*)
(defn prn-to-repl [& args]
(binding [*out* repl-out]
(apply prn args)))

现在,每当您想从不同的线程打印时,请使用:

(prn-to-repl "hello")

就是这样。希望这可以帮助。

关于emacs - Clojure 的线程在 Emacs clojure-repl 模式下不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com