Re: Extending Java class in Clojure with multiple constructors

2010-04-19 Thread David McNeil
Stephan- Thank you, gen-class is what I needed.

-David

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


Extending Java class in Clojure with multiple constructors

2010-04-18 Thread David McNeil
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


Re: Extending Java class in Clojure with multiple constructors

2010-04-18 Thread Stephan Mühlstrasser


On 18 Apr., 01:51, David McNeil mcneil.da...@gmail.com wrote:

 My question is: is there a better way to accomplish this, perhaps
 using a mechanism other than proxy?


You can do that with gen-class:
http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/gen-class.

... By default, constructors are created for the generated class
which
match the signature(s) of the constructors for the superclass. ...

So this is sufficient to create a derived class that inherits the
superclass constructors (save as file demo.clj also in package
demo) :

(ns demo.demo
  (:import
[demo Person])
  (:gen-class
   :extends demo.Person))

(println  (.toString (demo.demo. 15 joe)))

(println (.toString (demo.demo. 15)))

Regards
Stephan

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