On Aug 25, 9:54 pm, Meikel Brandmeyer <m...@kotka.de> wrote:
> 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?

What I meant is that if I always implement SignalX$Listener, I could
even get rid of the interfaces list as argument to the macro as it can
be derived from the number or arguments that will be passed to
trigger.
I could write
(create-listener [a1 a2 a3] ( do-stuf a b c ) )

and know that this has to proxy Signal3$Listener.
However, the bug linked makes that impossible for now as all nested
interfaces have the same name.

Thanks for your remarks!

Raphaël


>
> > * 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
> 2KViewDownload
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to