If I'm not mistaken, you should NOT use extend-type to extend java classes
(only interfaces and protocols).

To answer your questions, you can "extend" a class and interfaces with
proxy, like so:

(proxy [classA interfaceA interfaceB] [] ...)

Note that you cannot use protocols directly, you can however use the
interface that a protocol generates.

(ns user)
(defprotocol A)
(proxy [A] [])
=> java.lang.ClassCastException: clojure.lang.Var cannot be cast to
java.lang.Class
(proxy [user.A] [])
=> #<Object$A$ce2b2ade user.proxy$java.lang.Object$A$ce2b2ade@1520a48c>

So you don't need to fall back to gen-class (in most cases anyway). However,
proxy generates a new object, which means that creating new instances
requires some sort of copying. The way I usually solve this is to simply
make a function that calls proxy, e.g.

(defn some-fn []
   (proxy ...))

Jonathan

On Tue, May 17, 2011 at 11:52 PM, jlk <[email protected]> wrote:

> Hello
>
> Apologies if I'm misunderstanding something here, but is it possible
> to easily extend to a new java type?  For example:
>
> proxy -> allows you to implement methods that are already defined in
> an interface or class, so you can redefine methods
>
> (proxy [ClassA] []
> (thisMustBeDefinedInClassA [] ...)
>
> extend-type -> allows you to extend an existing java type
>
> (extend-type [String]
> AProtocol
> (nowAllStringsCanDothis [this] ...)
>
> but what about
>
> (proxy2  [String]
> AProtocol?
> (theseBehaveAsStringsWithThisAdded [] ...)
>
> leaving regular Strings unaffected?
>
> do I need to fall back to using gen-class?
>
> Thank you for any suggestions!
>
>
> - Lachlan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to