I think I have an interesting problem. I'm trying to create a macro
that generates a class AND a post-init method for the class. Something
like:

;;; create the macro
(defmacro definstance [name & body]
  (let [post-init (gensym "postinit")]
    `(do
       (defn ~(symbol (str "-" post-init)) [this#]
         (println "here we'll do something very interesting with
'body' "))
       (gen-class
         :name ~name
         :extends SomeSuperClass
         :post-init ~post-init
         ))))

;;; generate a class with it
(definstance somepackage.MyClass (println "here is the body"))

The macro returns a defn form, for defining the post-init, and a gen-
class form,  for defining the class itself. Since a macro can only
return one form, I wrapped the two forms in a single do form.

I can compile the above into a jar. I can then reference it from
another Java project and I can actually use the class. If I run the
Java project from the command line, everything works perfectly.

If I expand the macro with a (macroexpand '(definstance
somepackage.MyClass (println "here is the body"))), put the expansion
in a Clojure source file, and use that from a Java web project (from
within a Servlet) everything still works perfectly.

But when I try to use the above source directly from a web
application, I get an UnsupportedOperationException from the
constructor of the class, because it cannot find a method called
postinit31.

Summarised: the above code works as is in a Java command line
application. The explicit macro expansion works fine in a web
application called from a Servlet. But if I use the above code
literally in a web application, BANG.

It DOES work if I split the single macro into two macros, one for the
gen-class and one for the defn. But that is not what I want.

Can anybody help me out here?

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to