문제

I'm currently reading the Clojure Programming book, and following the examples. However I come across an error.

(defn print-logger
  [writer]
   #(binding [*out* writer]
      (println %)))

(def *out*-logger (print-logger *out*))

(*out*-logger "hello")

Will result in: `Can't dynamically bind non-dynamic var: user/out

I'm very new to Clojure, and don't understand why this happens, especially when I'm following the example :)

도움이 되었습니까?

해결책

The issue is not in the code sample you have.

*out* should refer to clojure.core/*out*, so if you are running this in a repl you might have run something previous, to what you have, like (def *out* something) to create a user/*out*.

Then, when you defined your print-logger function, the (binding [*out* writer] ...) statement would be trying to rebind user/*out* instead of clojure.core/*out*.


You can use ns-unmap to remove user/*out* from your namespace.

(ns-unmap 'user '*out*) ;; => nil

You will also need to define your print-logger function again to recapture the correct clojure.core/*out*.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top