I'm trying to write a helper macro for invoking a class's main.
Here's the macro:
(defmacro main [class & args]
#^{:doc "Invoke class's main with specified arguments, which are
automatically stringified"}
(if (= (count args) 0)
`(. ~class main (make-array String 0))
`(. ~class main (into-array (map str '~args)))))
Here is the intent. You pass a class symbol and an optional list of
arguments. The function stringifies the arguments and passes them to
the class's main. If the argument list is empty, it passes an zero-
length java.lang.String array to the main.
Ok, simple enough. Here's something to test it with:
package tango;
import java.util.Arrays;
public class Echo {
public static void main(String args[]) {
System.out.println("args=" + Arrays.asList(args));
}
}
Here's what happens:
user=> (main tango.Echo "a")
args=[a]
nil
user=> (main tango.Echo "a" "b" "c")
args=[a, b, c]
nil
user=> (let [c "value of c"] (main tango.Echo "a" "b" c))
args=[a, b, c]
nil
user=>
I expected the last output to be 'args=[a,b,value of c]'.
To take the extra Java class out of the loop, I wrote this second
macro:
user=> (defmacro s [& args] `(java.util.Arrays/asList (into-array (map
str '~args))))
#'user/s
user=> (s "1")
#<ArrayList [1]>
user=> (s "1" "blah")
#<ArrayList [1, blah]>
user=> (let [c "value of c"] (s "1" c))
#<ArrayList [1, c]>
user=>
I'm using revision 1235 with Java 1.6.0_10. Any suggestions?
Thanks,
Bill Smith
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---