Are circular references supported in clojure?

1. create two refs

user=> (def aref1 (ref {}))
#'user/aref1
user=> (def aref2 (ref {}))
#'user/aref2
user=> aref1
#<r...@98adae2: {}>
user=> aref2
#<r...@7b283052: {}>


2. alter ref1 to have a reference to ref2

user=> (dosync (ref-set aref1 {:a aref2}))
{:a #<r...@7b283052: {}>}
user=> aref1
#<r...@98adae2: {:a #<r...@7b283052: {}>}>
user=> aref2
#<r...@7b283052: {}>

3. alter ref2 to have a reference (pointer) to ref1

user=> (dosync (ref-set aref2 {:a aref1}))
{:a #<r...@98adae2: {:a #<r...@7b283052: {:a #<r...@98adae2: {:a #<r...@7b283052: {:a #<r...@98adae2: {:a #<r...@7b283052: {:a #<r...@98adae2: {:a #<r...@7b283052: {:a #<r...@98adae2: {:a #<r...@7b283052: {:a #<r...@98adae2: {:a #<r...@7b283052: {:a

<SNIP...>

{:java.lang.StackOverflowError

<SNIP...>

4. So, I've got a stack overflow... What's the proper way to deal with this? Are circular references like this not allowed?

5. I'm running into this b/c I'm trying to create two relationships for the Dining Philosophers problem:

A Table references it's seated philosophers and chopsticks.
A Philosopher references the table they are seated at and the left and right chopsticks on the table.
A Chopstick references either it's table or the Philosopher using it.

So, I have a mesh of references :-).

If you are interested, the code is at:

https://gist.github.com/757925

However, I'm still working on that code, and want to respond to Laurent and Orion on a previous thread once I understand how to handle (or not handle, perhaps) circular references.

-Todd

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