Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Macros are clearly a powerful tool, but with that power inevitably comes responsibility—and a list of caveats.
Macros operate at compile time. This means they are not first-class citizens of a running Clojure program like functions are. A macro has no access to runtime information, such as the current runtime values of a var. A macro sees only unevaluated data structures read from source code.
Consider a simple function that returns a greeting. It’s possible to write this as either a function or a macro:
(defn fn-hello [x] (str "Hello, " x "!")) (defmacro macro-hello [x] `(str "Hello, " ~x "!"))
These appear to behave similarly in some usages:
(fn-hello "Brian") ;= "Hello, Brian!" (macro-hello "Brian") ;= "Hello, Brian!"