Actions and bindings

6 views
Skip to first unread message

christop...@gmail.com

unread,
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).

Christophe

Rich Hickey

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


On Mar 14, 12:20 pm, "christo...@cgrand.net"
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
overhead.

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

Rich
Reply all
Reply to author
Forward
0 new messages