On Mar 1, 3:03 pm, "
smith49...@googlemail.com"
<
smith49...@googlemail.com> wrote:
> I was able to successfully derive from java.io.InputStream and
> override methods. But it seems to be impossible to add any new
> methods, i.e. methods that don't override parent methods. I reckon
> this is a limitation of dynamic proxies in general (right?) but it
> would have been nice to have that possibility e.g. to provide accessor
> methods to vars captured in a closure:
>
> (defn make-instance [arg]
> (proxy [my.parent.Class] []
> (get-arg [] arg)
> ...
>
It's not an inherent limitation of proxies, but I've left it out for
now for a number of reasons:
I want to cache the proxy classes rather than generate a new class
for each proxy call. If different proxy calls with the same supers
could have different method sets (they can't now, the method sets are
the union of the methods of the supers, regardless of which ones you
overide/implement), caching would be of limited use.
The dynamic update facility couldn't be extended to adding new
methods.
The generated proxy types are anonymous, so no one could know, in
a typed manner, about the additional methods - they would always have
to be called via reflection. Even though it supports it, Clojure is
not really a duck-typing oriented language.
Because of the previous consideration, it seemed to me the best thing
for someone to do would be to have an interface that described the
additional methods, and then just use that interface in the proxy. An
extremely useful general interface one might want to support in order
to extend access to possibly dynamic 'members' is IPersistentMap. Your
resulting proxy would work with all the map APIs:
(:arg instance-of-your-proxy)
See bean for an example.
> Nevertheless a nice new feature that helps a lot. Thanks!
>
You're welcome!
Rich