Actions and bindings

Skip to first unread message

Mar 14, 2008, 12:20:59 PM3/14/08
to Clojure
I know that threads do not inherit dynamic bindings but wouldn't
actions be an exception?
Since they are executing in a random thread of a thread-pool it
doesn't make sense to have per-thread binding. Does it?

> (def a (agent nil))
> #<Var: user/a>
> user=> (def b "root")
> #<Var: user/b>
> (binding [b "bound"] (send a (fn [av] (println b) (flush))))
> clojure.lang.Agent@18c6cbc
> root

If actions was allowed to inherit dynamic bindings, I would expect
this to print "bound".

What do you think? Is it a silly idea?
(Maybe cross-thread bindings inheritance is currently an expensive
operation, and, so, such a feature would go against the grain of
Clojure's design).


Rich Hickey

Mar 14, 2008, 5:00:54 PM3/14/08
to Clojure

On Mar 14, 12:20 pm, ""
The first thing to remember is that the second argument to send is a
regular fn object. It might be created in place, it might have been
created prior. The behavior needs to be the same in either case.

Second, if there was child-thread binding inheritance, it would occur
on thread creation. But the threads created for agents might a)
already exist, or b) be created outside the dynamic scope of the
action send.

Third, there might be many dynamic bindings, and an action might not
utilize any of them. Copying them in all cases would be significant

In any case, it is possible to create an action with precisely the set
of bound vars you want:

(send a (fn [av] (binding [b "bound"] (println b) (flush))))

If this becomes a common thing, a little macrology could make it

Reply all
Reply to author
0 new messages