Clojure thread does not show results in Emacs clojure -repl mode

I have this Clojure code that runs and executes a function.

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

However, when I run it in emacs in slime-repl mode (executed using cider-jack-in , nothing prints, but nil is returned.

enter image description here

With lein real I got the expected result.

 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 

What could be wrong?

+6
source share
1 answer

This is because the main thread in CIDER / Emacs associates the REPL output buffer with *out* dynamic var. This is why you can see things in emacs.

However, when starting a new thread, this binding does not exist. Fixing is quite simple - first create a function that will capture the current binding:

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

Now that you want to print from another thread, use:

 (prn-to-repl "hello") 

What is it. Hope this helps.

+9
source

Source: https://habr.com/ru/post/979668/


All Articles