On Sat, Dec 11, 2010 at 5:24 PM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Am 11.12.2010 um 23:10 schrieb Alexander Yakushev:
>> Oh, that's my fault, I tried with-meta function on the atom and it
>> wouldn't work. Still, after I defined an atom with some metadata in
>> it, how can I change it thereafter?
>
> I believe, you can't. You have to create a new atom.

You can "change" the metadata on the object held by the atom (if that
object supports metadata) via (swap! a with-meta ...).

One thing a bit annoying is if you want to alter the metadata in an
incremental way. To do that atomically requires a closure. Or defining
a swap-meta! function, like so:

(defn swap-meta! [a f & args]
  (swap! a
    (fn [x]
      (with-meta x (apply f (meta x) args)))))

That abstracts the "do it with a closure" method into a single function.

user=> (def x (atom (with-meta [] {:foo 1})))
#'user/x
user=> (meta @x)
{:foo 1}
user=> (swap-meta! x assoc :bar 2)
[]
user=> (meta @x)
{:bar 2, :foo 1}

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