[?] alter-var-root and *out*

50 views
Skip to first unread message

Philos Kim

unread,
Sep 1, 2021, 4:58:05 AMSep 1
to Clojure
I can change my dynamic var with alter-var-root like this.

(def ^:dynamic *x* "abc")
(alter-var-root #'*x* (constantly "xyz"))
*x*
; => "xyz"


However, I cannot change *out* with alter-var-root.

(alter-var-root #'*out* (constantly "xyz"))
*out*
; => #object[java.io.PrintWriter 0x6957ec80 "java.io.PrintWriter@6957ec80"]


I expected the result to be "xyz" but the result of *out* doe not change at all

Can anyone explain to me why?

Paul Stadig

unread,
Sep 1, 2021, 7:38:39 AMSep 1
to clo...@googlegroups.com
`*out*` may have a thread binding established. REPLs will often do this.

If `*out*` is thread bound you should be able to use `thread-bound?` to check that and then `set!` to set it, but you have to be careful because https://clojure.atlassian.net/browse/CLJ-1077

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/23d13e9b-6eb8-4a39-b00d-8fead541e18en%40googlegroups.com.

Benedek Szilvasy

unread,
Sep 1, 2021, 8:55:44 AMSep 1
to clo...@googlegroups.com
You are altering the root binding, rather than the current (possibly dynamic) binding of the variable. The two may be distinct, since the current bindings may be thread local, such as by `clojure.core/binding` (see https://clojure.org/reference/vars).

Note that you can `alter-var-root` of non-dynamic vars too, though you should be careful with that (and usually avoid it altogether).
Reply all
Reply to author
Forward
0 new messages