> I'm a clojure newbie trying to understand when to use atoms versus refs.
> In clojure-http.resourcefully, if you use the with-cookies macro stores the
> cookies in a thread local ref.  Why a ref and not an atom?  My impression is
> that refs are for when you need to coordinate changes to more than one
> variable, or maybe if you need triggered validations.

I looked at:

   
http://github.com/technomancy/clojure-http-client/blob/master/src/clojure_http/resourcefully.clj

Without making claims to the intent of the original author, my
interpretation is that *cookies* is a globally shared resource (a
namespace-global var), and thus a ref is used for co-ordinated
updates.

In the case of with-cookies, the intent is a thread-local binding
overriding the cookies. If it were not still bound to a var,
(save-cookies..) and the code in the define-method macro would have to
support *cookies* being either a ref or a non-ref (in addition to
'nil' which is already special-cased).

My guess is that it is just the simpler way to implement it, and it
also means the same code paths are taken regardless of whether or not
you're inside (with-cookies ...). Presumably HTTP is expensive enough
anyway that eliminating the overhead associated with a ref can be
considered an irrelevant overhead.

-- 
/ Peter Schuller

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to