I believe I understand that during macro expansion, a macro does not have access to the things a function has access to, because expansion happens before compile time.
But, I am having trouble understanding is how to perform checks at macro expansion time.
For example:
(defn gen-class [cl-nam]
(fn [cmd & args]
(condp = cmd
:name (name cl-nam))))
(defmacro defnclass [cl-nam]
`(def ~cl-nam (gen-class '~cl-nam)))
I would like to check to see that cl-nam is not a sequence. I would like to use count and find out of its length is > 1.
I understand I can unquote the println in the following macro, so that I can get an expansion-time message.
(defmacro defnclass_info [cl-nam]
`(do
~(println cl-nam)
(def ~cl-nam (gen-class '~cl-nam))))
But, I am not sure how to go about checking to see what was passed for cl-nam.
I’m reading a lot of Clojure macro descriptions from several books, and am stumped.
Thanks.
A macro is really just a function.
Edit: One could also switch behaviour and recursively call the macro again for the whole list. What you do, depends on your requirements.
The macro expansion is just what the “function” returns. So you have full flexibility on what you do with the macro arguments.