Because of identity-value indirection, there's nothing to it:

(defn swap-refs! [r1 r2]
  (let [x @r1]
    (ref-set r1 @r2)
    (ref-set r2 x)))

In languages like Python and Smalltalk that conflate value and
identity, you need nasty tricks like abusing the garbage collector's
object graph traverser to find all references to a given object (e.g.
gc.get_referrers() in Python) and then rewire those references on the
fly.

-Per

On Mon, Apr 5, 2010 at 9:17 PM, Douglas Philips <d...@mac.com> wrote:
> On 2010 Apr 5, at 9:43 AM, Per Vognsen wrote:
>>
>> I already mentioned this in my original response, but it's worth
>> reiterating that the situation is very much like a relational database
>> system. In a relational database there are no direct pointers, only
>> primary keys. A primary key is nothing more than an identifier; it
>> becomes a specific reference only in the context of a table.
>>
>> So despite initial appearances it's actually a very familiar programming
>> model.
>
> It is too bad that clojure doesn't do that uniformly, otherwise I might get
> used to having sets of sets instead of sets of handles to sets that I have
> to dereference manually, as I would do with a database.
>
> Smalltalk has become: to address entire classes of problems like this one.
> (a quick/modern overview of become:
> http://gbracha.blogspot.com/2009/07/miracle-of-become.html)
>
> If clojure had become (I don't know enough about internals), you could do:
> start transaction
> Make A
> Make B, referring to A.
> Make A', referring to B.
> A become: A' (swap the contents, but not the identity)
> end transaction.
>
> The transactional isolation prevents A and B from being visible before they
> are finished.
>
> Reifying the relationship would be another kind of indirection also commonly
> used in databases.
>
> -Doug
>
> --
> 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
>
> To unsubscribe, reply using "remove me" as the subject.
>

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