Clojure

GC Issue 45: :post-init option for gen-class

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Reported by chouser, Jan 15, 2009
Describe the feature/change.

It's currently impossible to create a class with gen-class that calls
its own (or its super-classes') methods when it is constructed.  This
is a problem if there is stateful initialization required by the
superclass for which it has only methods, not ctor args.

Was this discussed on the group? If so, please provide a link to the
discussion:

http://groups.google.com/group/clojure/browse_thread/thread/d1fbdb7450b46dee/b5539e4a21c411c6
http://clojure-log.n01se.net/date/2009-01-15.html#10:22-10:37a

Comment 1 by chouser, Mar 07, 2009
(No comment was entered for this change.)
 gen-class-post-init.patch
3.7 KB Download
Comment 2 by cjkent, Mar 24, 2009
I've applied the patch and it works, but only if I define a post-init function with
no args.  I was expecting at least a single "this" arg.  Or is there another way to
get a reference to the newly created object?
Comment 3 by chouser, Mar 24, 2009
The args given to your class's constructor are passed directly to your post-init
function.  But you have a good point -- the 'this' arg should probably be included. 
I've attached a new patch that allows this:

(ns clojure.contrib.test-clojure.gen-class
  (:gen-class
     :extends javax.swing.border.TitledBorder
     :constructors {[int int String] [String]}
     :init my-init
     :post-init after-ctor))

(defn -my-init [x y s]
  (println "Init ignoring" x "and" y ", going with" (str "foo+" s))
  [[(str "foo+" s)] nil])

(defn -after-ctor [this x y s]
  (print "Constructing" this "with ")
  (prn x y s))

user=> (compile 'clojure.contrib.test-clojure.gen-class)
clojure.contrib.test-clojure.gen-class

user=> (clojure.contrib.test_clojure.gen_class. 3 4 "hello")
Init ignoring 3 and 4 , going with foo+hello
Constructing #<gen_class clojure.contrib.test_clojure.gen_class@1ab2b55> with 3 4 "hello"
#<gen_class clojure.contrib.test_clojure.gen_class@1ab2b55>

 post-init.patch
3.8 KB Download
Comment 4 by cjkent, Mar 25, 2009
Great, that works perfectly.  Thanks.
Comment 5 by richhickey, Apr 12, 2009
Patch applied - r1348 - thanks!
Status: Fixed

People

  • Assignee:
    Unassigned
    Reporter:
    Anonymous
Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: