Frage

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 :)

War es hilfreich?

Lösung

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*.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top