Correct.
My understanding is that the init function is actually run before the
instance is even created. So no only is there no "this" parameter,
there's not even a "this" value yet.
Instead, your init function returns values to pass to the superclass
constructor and the value to use as your object's state. Also note
that I had to specify :constructors in order for my init to accept no
args but to use the constructor of the superclass that takes a String.
(ns net.n01se.MyThread
(:gen-class
:extends "java.lang.Thread"
:constructors {[] [String]}
:init my-init
:state myState
:exposes-methods {getId getIdSuper}))
(defn -my-init []
(prn :init-my-thread)
[["my derived class"] "this is my state"])
(defn -getId [this]
(+ 123000 (.getIdSuper this)))
This also demonstrates the use of :exposes-methods to call the
superclass's implementation of a method I'm overridding.
Note that because I didn't use a mutable object for myState, (instead
using the string "this is my state"), there's no way to change the
value of myState.
user=> (compile 'net.n01se.MyThread)
net.n01se.MyThread
user=> (def x (net.n01se.MyThread.))
:init-my-thread
#'user/x
user=> (.myState x)
"this is my state"
user=> (.getId x)
123008
user=> (.getName x)
"my derived class"
--Chouser
The example I posted does exactly this -- the name of all MyThreads
start as "my derived class" as demonstrated by:
Seems like that class has a rather unfortunate API. :-)
As far as I know, this is not currently possible with gen-class.
Seems like you'd need some kind of post-constructor hook, provided
just to deal with mutable state of the base class.
I wonder if Rich would accept a patch providing an :after-ctor option
to gen-class.
--Chouser
I've posted a feature request:
http://code.google.com/p/clojure/issues/detail?id=45
Now you can submit your CA, submit a patch, and nobody will ever have
to suffer your pain again. :-)
--Chouser