On Thu Mar  1 21:53 2012, sim wrote:
> Hi all,
> 
> This has me stumped and I found another message: 
> https://groups.google.com/forum/?fromgroups#!searchin/clojure/parameterised$20type/clojure/8YxzIYXH49c/xCxkMaGXBzUJ
>  
> 
> that says it isn't possible, but that was back in 2009, is it still not 
> possible?  Any work arounds apart from doing some of it in Java?
> 
> I would like to be able to write some xWiki Macros in clojure, I have some 
> in java already but moving it all to clojure would be better.
> 
> The java example is as follows:
> 
> public class ExampleMacro extends AbstractMacro<ExampleMacroParameters> 
> 
> Which is from here: 
> http://rendering.xwiki.org/xwiki/bin/view/Main/ExtendingMacro 
> 
> Any help/pointers would be greatly appreciated.
> 
> -- sim

Yes, it is possible.  At the byte-code level there is (at least at
run-time) no difference between a generic and a non-generic class.  All
methods take/return the generic type just use Object.

For example, take the following implementation of AbstractCollection<E>:

--- begin  ExampleCollection.clj ---

(ns ExampleCollection
  (:gen-class :extends java.util.AbstractCollection))

(def some-numbers [2 3 5 7 11 13]) 
      
(defn -size [_] (count some-numbers))
            
(defn -iterator [_] (.iterator some-numbers))


--- end  ExampleCollection.clj ---

You can use it as in the following:

--- begin Example.java ---
import java.util.Collection;
  
public class Example {
  public static void main(String[] args) {
    Collection<Long> foo = new ExampleCollection();
    for (Long bar : foo) {
      System.out.println(bar);
    }
  }
}
--- end Example.java ---

The Java file will compile and run, though it will complain about doing
an unsafe conversion.  This means that if the type that occurs at
runtime is wrong, you'll get a ClassCastException.

In any case, you can also use proxies to accomplish the same task if you
don't want to gen-class.

I hope this helps.

Sincerely,

Daniel

Attachment: signature.asc
Description: Digital signature

Reply via email to