On 11/17/11 4:21 AM, Chas Emerick wrote:
Ben,
It's a good idea, but it's going to bite you:
- what if the class in question has fields or package-private methods that are
touched by Java code?
Yeah, there are some use cases where this would fall apart in. Assuming
that you use only public methods then everything should delegate as
expected. Point taken about the potential of unpleasant surprises.
- what if the class take ctor arguments?
- what if the ctor has side effects?
I don't think these last two points apply.. in my use case the object
has already been created and so the original constructor never is
called. The instantiated object is simply passed from one delegate to
another with the different metadata associated on to it.
-Ben
On Nov 16, 2011, at 11:28 AM, Ben Mabey wrote:
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
--
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