Browse Learn Clojure Foundations as a Java Developer

Write Basic Clojure Macros Safely

Learn the small set of macro-writing tools Java engineers need first: defmacro, syntax quote, unquote, unquote-splicing, auto-gensyms, and expansion review.

The safest way to write a macro is to start with a function and move to defmacro only when you need syntax or evaluation control.

Basic macro writing is mostly about three habits:

Habit Why it matters
Build a tiny expansion first. The generated code should be boring and reviewable.
Use syntax quote, unquote, and splicing deliberately. These decide which parts are templates and which parts come from the caller.
Check macroexpand-1 before adding features. Expansion review catches multiple evaluation, name capture, and hidden control flow.

For Java engineers, treat a macro like source generation inside the language. The call site may be compact, but the expansion is the real code your team must understand.

In this section

  • Define Clojure Macros with defmacro
    Learn how defmacro receives unevaluated forms, returns generated Clojure code, and should be reviewed with macroexpand before Java teams rely on a custom syntax form.
  • Build a Small Clojure Macro Example
    Walk through a small timing macro that shows when macro syntax is useful, how to keep generated code readable, and how Java engineers should review the expansion before using it.
  • Use Quote and Unquote in Clojure Macros
    Learn how quote, syntax quote, unquote, unquote-splicing, and auto-gensyms work together to build readable Clojure macro templates without variable capture.
Revised on Saturday, May 23, 2026