Macro expansion transforms a macro invocation into the expression that will be evaluated, without evaluating the resulting form. Use it at the REPL to inspect behavior, understand generated code, or debug macros. section 4.2
Choose the expansion function according to the depth you need:
- macroexpand-1 expands only the outermost form once. section 4.2
- macroexpand repeatedly expands the outermost form until it is no longer a macro, without entering inner forms. section 4.2
- clojure.walk/macroexpand-all expands outer forms and macro invocations nested inside them. section 4.2
Compare expansion depth
(macroexpand-1 '(when-first [a [1 2 3]] (println a))) ;; expands the outer when-first once (macroexpand '(when-first [a [1 2 3]] (println a))) ;; continues expanding the outer generated forms (clojure.walk/macroexpand-all '(when-first [a [1 2 3]] (println a))) ;; also expands nested when, let, and related forms The input must be a valid Clojure expression; each function returns a form rather than evaluating it. section 4.2
Inspect short-circuiting
(clojure.walk/macroexpand-all '(and false true true)) ;; reveals nested let* and if forms Expansion makes the control flow behind macros such as and visible, including their short-circuit behavior. section 3.2.2
Listing 4.2. → Macro inspection, REPL util, Debugging
(macroexpand form) (macroexpand-1 form) (clojure.walk/macroexpand-all form)
Browse the compact REPL reference for the three macro inspection functions. section 4.2



