On Tue, May 17, 2011 at 5:52 PM, jlk <lachlan.kana...@gmail.com> 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!

You can use gen-class to extend existing Java classes and add new
methods. But not String. String is final and the JVM enforces that it
can't be subclassed (unlike, say, checked exceptions, which javac
enforces but the JVM does not, hence why Clojure can lack that
"feature").

What you can do is extend CharSequence to a new String-like class and
use that. The problem is that you won't be able to pass it to
functions that hint it as String and call String methods on it without
getting a CCE. Anything that assumes merely CharSequence is fine
though.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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