[Context recovered from top-posting.]

On Sun, 21 Mar 2010 10:31:58 -0700
Josh Stratton <strattonbra...@gmail.com> wrote:
> On Sat, Mar 20, 2010 at 8:51 AM, Mike Meyer
> <mwm-keyword-googlegroups.620...@mired.org> wrote:
> > On Sat, 20 Mar 2010 08:11:49 -0700 (PDT)
> > strattonbrazil <strattonbra...@gmail.com> wrote:
> >
> >> I'd like to separate my ui Swing/JOGL from the content, so my code is
> >> relatively unaware of the UI around it.  For example, I create a
> >> global context that holds on my content.  I then make a UI that when
> >> the user does some interaction like a mouse click or drag, the UI
> >> creates a new context.  My OO instincts would be to create a context
> >> and pass it to all my UI objects that receive events and mutate them,
> >> but if I'm dealing with an immutable class if I pass it to each UI
> >> object, when one UI object makes a new context, it isn't reflected in
> >> the other UIs.  Basically I want all UIs pointing to the same context
> >> and each UI being able to create a new context that each UI points
> >> to.  It seems that when one UI updates the global context reference
> >> with a 'def', the others are still using the old one.
> >
> > You need to update shared data, and that doesn't seem avoidable. In
> > clojure, you do that with either a ref or an atom holding the data,
> > depending on how you need to update it. This will make it thread safe
> > if/when you start running your UI objects in different threads.
> >
> An atom seems appropriate after reading up on it, but documentation
> seems a little scarce.  Reading about atoms on the clojure site gives
> a very small example using just a {}.  I've tried piecing it together
> using a struct but am getting
> 
> Exception in thread "main" java.lang.IllegalArgumentException: Key
> must be integer (start.clj:0)
> 
> Can atoms be only certain types?
> 
> let [context (atom (create-context))] ; create-context returns a context 
> object
>   (def canvas0 (create-canvas 0 context)) ; passing the atomized
> context to a struct
>   )

Atoms can only be one type: atom. They can hold objects of arbitrary
type.  Context is an atom. If create-canvas expects some other type,
it won't work. You have three options: 1) create-context doesn't
modify the context; just pass in @context to pass in the context the
atom holds. 2) create-context modifies the context, so either a) it
needs to be modified to expect an atom and change it appropriately, or
b) it needs to be modified to return the new value of the context so
it can be used with one of the atom-changing functions.

        <mike
-- 
Mike Meyer <m...@mired.org>             http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
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 from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to