Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure Proxy Java Templates

Tags:

clojure

Context

I have this piece of Java Code

    btn.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent event) {
            System.out.println("Hello World");
        }
    });

I want to convert it into Clojure.

What I know

(. btn setOnAction (proxy .... ????? .... ))

Question:

How do I handle EventHandler part in Clojure? I believe this is a Java Template. How do I create templated objects in Clojure?


1 Answers

In Java they're called Generics, not Templates. Furthermore, they're implemented using type erasure, i.e. there's no generic information in the compiled bytecode, so that EventHandler<Foobar> objects are compiled to be non-generified EventHandler instances.

That said, Clojure doesn't even try to support them. Your Java code translates into

(.setOnAction btn 
  (proxy [EventHandler] []
    (handle [event]
      (println "Hello World"))))

See the documentation on proxy and on Java interop for more details on the syntax.

like image 195
skuro Avatar answered Nov 27 '25 06:11

skuro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!