On Jun 2, 2009, at 3:35 AM, Adrian Cuthbertson wrote:
Thanks Steve! That's very neat. Pretty much a "canonical" macro example.
You're welcome! I'm glad you like it. On Jun 2, 2009, at 4:45 AM, Konrad Hinsen wrote:
That looks like a perfect candidate for clojure.contrib.repl-utils!
Thanks! After a little more discussion, I'd like to put it in there.After thinking about other macro advice I've seen (from Meikel Brandmeyer and elsewhere), I came up with this version that does most of the work in a function and provides the macro just to control the evaluation of arguments:
(defn run*
"Loads the specified namespace and invokes its \"main\"
function with optional args."
[ns-sym & args]
(require ns-sym :reload-all)
(apply (ns-resolve ns-sym 'main) args))
(defmacro run
"Loads the specified namespace and invokes its \"main\"
function with optional args. ns-name is not evaluated."
[ns-name & args]
`(run* '~ns-name ~...@args))
I see the tradeoffs as:
possible negative:
- an extra name in the namespace
positives:
- the code is easier to understand
- the function version composes better: it's easier to call from
other code if that were desired.
Overall I like the function/macro pair version better. Any thoughts? --Steve
smime.p7s
Description: S/MIME cryptographic signature
