[Solved] Re: Invoking Java method through method name as a String

2009-02-22 Thread Richard Lyman
I've added a section on the Wiki under the examples: http://en.wikibooks.org/wiki/Clojure_Programming/Examples#Invoking_Java_method_through_method_name_as_a_String Again, thanks! -Rich --~--~-~--~~~---~--~~ You received this message because you are subscribed to t

Re: Invoking Java method through method name as a String

2009-02-22 Thread Richard Lyman
This is _so_ awesome!! Thanks a ton for your patient help Tim, and others. In the end I also had to switch the use of into-array to to-array, since into-array expects all the elements to be the same type, and my args were of varying types. Using to-array worked since it cast each element to the O

Re: Invoking Java method through method name as a String

2009-02-22 Thread Timothy Pratley
> I guess I'm still stuck on how to expand the vector of arguments in place... One way is (apply function vector) -> (function v1 v2 v3) > and I'm really not very sure what you're doing with the '&' in the > parameters for the str-invoke. Is that a way of slurping all the remaining > parameters

Re: Invoking Java method through method name as a String

2009-02-21 Thread Michael Wood
On Sun, Feb 22, 2009 at 4:44 AM, Richard Lyman wrote: [...] > user=> (defn str-invoke [instance method-str & args] > (clojure.lang.Reflector/invokeInstanceMethod instance method-str (into-array > args))) > #'user/str-invoke > user=> (def i "sampleString") > #'user/i > user=> (def m "substring") >

Re: Invoking Java method through method name as a String

2009-02-21 Thread Richard Lyman
Cool... that looks like it got me past the 'method-as-a-string' problem, but now I'm getting errors with not finding a matching method on the class by that name. I'm guessing that this means there was a problem matching the type signature of the method. The arguments to the method are stored in a

Re: Invoking Java method through method name as a String

2009-02-21 Thread Timothy Pratley
How embarrassing! This works much better: (defn str-invoke [method-str instance & args] (clojure.lang.Reflector/invokeInstanceMethod instance method-str (to- array args))) (let [ts "toString", ct "compareTo"] (println (str-invoke ts 5)) (println (str-invoke ct 5 4)) (println (str-invoke

Re: Invoking Java method through method name as a String

2009-02-21 Thread Richard Lyman
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 u

Re: Invoking Java method through method name as a String

2009-02-21 Thread Timothy Pratley
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 be

Re: Invoking Java method through method name as a String

2009-02-21 Thread pmf
On Feb 21, 8:31 am, Richard Lyman wrote: > I have an instance of the Java class in a variable. > I have the method arguments in a vector. > I have the method name as a String. > > I've tried so many different ways to invoke that method on that class and > pass those parameters. I've tried macros,

Invoking Java method through method name as a String

2009-02-21 Thread Richard Lyman
I have an instance of the Java class in a variable. I have the method arguments in a vector. I have the method name as a String. I've tried so many different ways to invoke that method on that class and pass those parameters. I've tried macros, reflection, and read/eval. None of the ways I've trie