Hi,
I would like to be able to add metadata to arbitrary java objects that have already been instantiated. I know that you can use proxy to add metadata to objects that you create but in my case the object already exists (i.e. it is returned from another method call outside of my control).

It seems like the best solution would be to create a delegate class/object that wraps the original one. Being able to write something like this would be ideal:

(defn wrap-with-meta
  ([obj]
     (wrap-with-meta obj nil))
  ([obj meta]
   (delegate obj
             clojure.lang.IObj
(withMeta [this new-meta] (wrap-with-meta obj new-meta))
             (meta [this] meta))))

The delegate function would operate very similar to proxy, but instead of taking a class it takes an object. A class would be created that extends the object's class, just like a class is generated for proxy. However, instead of stubs being generated that call super the stubs would delegate to the given object. (Also note, I am also using reify-like syntax since I prefer that to the syntax in proxy.)

I'm curious to hear people's thoughts on this approach. I'd also be really interested to see how other people have addressed this in the past (I doubt I'm the first one to run into this).

I know that clojure mentality is to avoid wrappers, but I don't see this as being much different than what proxy already does. Of course, I may be missing something... if so, please enlighten me. :)

Thanks,
Ben

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