Hi, Am 25.08.2009 um 20:09 schrieb rb:
Here is what I've done for now: (defmacro deflistener [ interfaces trigger-args & body] `(proxy [ ~...@interfaces ] [] (trigger [...@trigger-args] ~...@body)) ) Which can be used like this: ( deflistener (Signal1$Listener) (mouse-event) (.setText result-text (.getText line-edit)) ))
I would rewrite it as follows:
(defmacro create-listener
[interfaces args & body]
`(proxy ~interfaces [] (trigger ~args ~...@body)))
First the macro doesn't "def" something global. So it should not be
called deflistener. Compare with create-struct and defstruct.
Then with the above changes the call looks like
(create-listener [inter faces] [a b c] (do-stuff a b c))
This is more idiomatic Clojure. (Effectively you can specify a vector
in your version also, but seems like waste to define vector which is
thrown away just to define another vector with the same contents. I
find this not very elegant)
It takes as arguments * the list of interfaces it implements (currently necessary for http://www.assembla.com/spaces/clojure/tickets/181)
?? You always have to specify the interfaces, no?
* the list of arguments passed to the trigger method implemented * the body of the implementation of trigger Here are the questions that arise: * I thought I could and should do away with trigger-args, but if I use gensyms in the macro to define the arguments received by trigger, how can I get to the argument values in the code of the method?
You cannot get rid of the argument vector.
* if it implements SignalX$Listener, trigger takes X arguments. How can implement that with one macro, and still have access to these arguments in the forms I pass as body of trigger?
You could pass the body as a fn.
(defmacro create-listener
[x f]
(let [args (take x (repeatedly gensym))]
`(proxy [~(symbol (str "Signal" x "$Listener"))] [] (trigger
[...@args] (~f ~...@args)))))
And call it like
(create-listener 2 (fn [a b] ...))
Whether this is better than the above.... I would prefer the first
solution.
Hope this helps. Sincerely Meikel
smime.p7s
Description: S/MIME cryptographic signature
