When extending a Java class in Clojure, what is a clean way to support
multiple constructors in the base class? Below is a sample of how I
accomplished this. The key point is that the base class has two
constructors and to expose both of these in Clojure I needed to create
two proxies. I used a macro to avoid duplicating the proxy definition.
My question is: is there a better way to accomplish this, perhaps
using a mechanism other than proxy?

Thank you.
-David McNeil

====
Person.java
=====
package demo;

public class Person {
    private int id = 0;
    private String name;

    public Person( int id ) {
        this.id = id;
    }

    public Person( int id, String name ) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return "Person id=" + id + " name=" + name;
    }
}

====
demo.clj
====

(ns clojure-proxy.demo
  (:import (demo Person)))

(defmacro sub-person [& args]
   `(proxy [Person] [...@args]
      (toString [] (str "overridden toString"))))

(defn to-person
  ([id]
     (sub-person id))
  ([id name]
     (sub-person id name)))

(.toString (to-person 15 "joe"))

(.toString (to-person 20))

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