Thanks for the quick response! :-)

This works fine when the method name is not in a var, but if you try:

user=> (defmacro my-invoke [method-str instance & args]
 `(. ~instance ~(symbol method-str) ~...@args))
nil
user=> (my-invoke "toString" 5)
"5"
user=> (def command "toString")
#'user/command
user=> (my-invoke command 5)
java.lang.IllegalArgumentException: No matching field found: command for
class java.lang.Integer (NO_SOURCE_FILE:0)
user=>


The macro is processed before the expression is eval'd - right? That's why
the call to symbol inside the macro returns 'command' as the method you're
invoking - right?

I'm not sure I'm understanding everything here, but it seems like I'm
needing a way to delay the evaluation of that part of the macro until the
value bound to the command var can be inserted...

-Richard




On Sat, Feb 21, 2009 at 6:15 AM, Timothy Pratley
<timothyprat...@gmail.com>wrote:

>
> Hi Richard,
>
> As you probably know, Clojure java interop requires the method to be a
> symbol:
> user=> (. 5 toString)
> "5"
>
> So if you want to invoke a method from a string, you can convert the
> string to symbol first:
> user=> (symbol "toString")
> toString
>
> Great! but (. 5 (symbol "toString")) wont work because (symbol
> "toString") is not evaluated. Instead it gets transformed into
> 5.symbol("toString") which is not what we want :( :(
> However with a little trickery we can still do it (please excuse
> ugliness - my macro-fu is weak):
>
> (defmacro my-invoke [method-str instance & args]
>  `(. ~instance ~(symbol method-str) ~...@args))
>
> user=> (my-invoke "toString" 5)
> "5"
>
>
>
> Regards,
> Tim.
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
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