There are several somewhat old blog posts out there advising caution when mixing dynamic variables, binding
, and pmap
, e.g. here, where we get the following code snippet:
user=> (def *foo* 5)
#'user/*foo*
user=> (defn adder
[param]
(+ *foo* param))
#'user/adder
user=> (binding [*foo* 10]
(doseq [v (pmap adder (repeat 3 5))]
(println v)))
10
10
10
nil
But that's not what happens when I run that code (changing the first line to (def ^:dynamic *foo* 5)
). I get three 15
s as output (using Clojure 1.4), just as you would naïvely expect—that is, with the change in the binding form seen by the function passed to pmap. Have the way thread-local bindings and pmap interact changed? I can't find this documented anywhere.