On Jun 18, 1:25 pm, Adam Blinkinsop <bli...@acm.org> wrote:
> I'm looking through the primitives that Clojure has for STM with refs, and
> it seems nice and minimal, except for these two functions, which I can't
> tease apart.  From the documentation,
>
>  *(ref-set ref val)*
>
> Must be called in a transaction. Sets the value of ref. Returns val.
>
>  *(alter ref fun & args)*
>
> Must be called in a transaction. Sets the in-transaction-value of ref to:
>
> > (apply fun in-transaction-value-of-ref args)
> > and returns the in-transaction-value of ref.
>
> Does ref-set not set the "in-transaction-value"?  It looks like the only
> difference is the signature, and that can't be right.

Looks to me like they are analogous to reset! and swap! on atoms.

(swap! atom fn & args) is shorthand for (reset! atom (fn @atom &
args)) with the added, crucial ingredient of atomicity; nothing else
can change the atom in between the deref and the reset if you use
swap!.

With transactions, the difference is less stark. It just saves you
some typing: (alter ref fn & args) replaces (ref-set ref (fn @ref &
args)), or maybe (ref-set ref (fn (ensure ref) & args)). Atomicity is
unaffected since the transaction as a whole is committed atomically,
transactions are thread-local, and the in-transaction value of ref is
used in all three cases.

--~--~---------~--~----~------------~-------~--~----~
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