Charles Duffy
Charles Duffy

Reputation: 295262

Updates to thread-local bindings in Clojure done with set! not taking place?

I'm trying to use set! to modify the thread-local bindings of a var -- but these changes don't appear to be taking effect. Consider the following:

(def ^:dynamic *foo* :root)
(binding [*foo* :thread-local]
  (let [val (doto :new
               #(set! *foo* %))]
    [val *foo*]))

I would expect the result to be [:new :new]; instead, this evaluates to [:new :thread-local]. What am I misunderstanding here?

Upvotes: 2

Views: 159

Answers (1)

kotarak
kotarak

Reputation: 17299

Your doto syntax is wrong.

(binding [*foo* :thread-local]
  (let [val (doto :new (#(set! *foo* %)))]
    [val *foo*]))

Note the additional parens.

Upvotes: 2

Related Questions