Clojure Guides - Language - Macros
Clojure Guides - Language - Macros
Clojure macros
the Clojure compilation process
This work is licensed under a Creative Commons Attribution 3.0 Unported License
(https://creativecommons.org/licenses/by/3.0/) (including images & stylesheets). The source is available
on Github (https://github.com/clojure-doc/clojure-doc.github.io).
If some parts are not clear, please ask for clarification in the #clojure-doc channel on Slack
(https://clojurians.slack.com/archives/C02M6N5C137) (self-signup at clojurians.net (http://clojurians.net))
or file an issue (https://github.com/clojure-doc/clojure-doc.github.io/issues) on GitHub. We will work hard
on making this guide easy to follow with edits and images to illustrate the concepts.
Overview
Clojure is a dialect of Lisp and while it departs from some features of "traditional" Lisps, the fundamentals
are all there. One very powerful feature that comes with Lisps is macros, a way to do metaprogramming
using the language itself. This is pretty different from other languages known for good metaprogramming
capabilities (e.g. Ruby) in that in Clojure, metaprogramming does not mean string generation. Instead, it
means constructing a tree [of S-expressions, or lists]. This enables very powerful DSLs (domain-specific
languages).
Clojure code is compiled when it is loaded with clojure.core/require (or other, lower-level functions
like clojure.core/load-file ). Clojure code can also be compiled ahead of time (referred to as "AOT
compilation") using tools such as tools.build (https://clojure.org/guides/tools_build), via the Clojure
CLI, or Leiningen (https://leiningen.org).
Clojure Reader
Reader is another name for parser. Unlike many other languages, the reader in Clojure can be extended
in the language itself. It is also exposed to the language with clojure.core/read and
clojure.core/read-string functions that return data structures:
The reader produces data structures (in part that's why "code is data" in what we refer to as "homoiconic
languages") which are then evaluated:
Expressions that can be evaluated (invoked) are known as forms. Forms consist of:
Functions
Macros
Special forms
Special Forms
The reader parses some forms in special ways that are not consistent with the rest of Clojure's syntax.
Such forms are called special forms. Commonly used examples include:
def
if
do
let
loop
recur
Some of these special forms are actually macros that expand to underlying special forms that the compiler
implements directly, but that should be considered an implementation detail ( loop is implemented as a
macro on top of loop* , for example).
#'cljs.user/unless
Macros are defined using the clojure.core/defmacro function that takes macro name as a symbol, an
optional documentation string, a vector of arguments and the macro body.
(unless (= 1 2)
"one does not equal two"
"one equals two. How come?")
Just like the if special form, this macro produces an expression that returns a value:
(unless (= 1 2)
"one does not equal two"
"one equals two. How come?")
in fact, this is because the macro piggybacks on the if form. To see what the macro expands to, we can
use clojure.core/macroexpand-1 :
Note: Clojure on the JVM would expand to a call to clojure.core/not here but the interactive
examples use Klipse which runs as ClojureScript in the browser, so it expands to cljs.core/not
instead.
This simplistic macro and the way we expanded it with macroexpand-1 demonstrates three features of
the Clojure reader that are used when writing macros:
Quote (')
Syntax quote (`)
Unquote (~)
Unquote splicing (~@)
Quote
Quote suppresses evaluation of the form that follows it. In other words, instead of being treated as an
invocation, it will be treated as a list.
Compare:
;; this form is evaluated by calling the clojure.core/+ function
(+ 1 2 3)
(+ 1 2 3)
Syntax Quote
Syntax quote also suppresses evaluation of the form that follows it but allows for substitution of parts of
that form using unquote ( ~ ). It is similar to templating languages where parts of the template are "fixed"
and parts are "inserted" (evaluated). The syntax quote makes the form that follows it "a template".
(cljs.core/+ 1 2 3)
Unquote
Unquote is how parts of the template are evaluated (like variables in templates in templating languages).
(defmacro unless
[condition & forms]
`(if (not ~condition)
~@forms))
#'cljs.user/unless
(unless (= 1 2)
"one does not equal two"
"one equals two. How come?")
When the macro is expanded, the condition local in this example has the value of (= 1 2) (a list). We
want to substitute the value of condition into the if form in our template, and that's what unquote ( ~ )
does as can be seen from macroexpansion:
(macroexpand-1 '(unless (= 1 2) true false))
Compare this with what the macro expands to when the unquote is removed:
Implementation Details
The unquote operator is replaced by the reader with a call to a core Clojure function,
clojure.core/unquote .
Unquote-splicing
Some macros take multiple forms. This is common in DSLs, for example. Each of those forms is often
need to be quoted and concatenated.
The unquote-splicing operator ( ~@ ) is a convenient way to do it, unrolling a collection of forms into the
expanded code:
(defmacro unsplice
[& coll]
`(do ~@coll))
#'cljs.user/unsplice
#'cljs.user/b
1
b
Implementation Details
The unquote-splicing operator is replaced by the reader with a call to a core Clojure function,
clojure.core/unquote-splicing .
Clojure does not implement a full solution to hygienic macros but provides solutions to the biggest pitfalls
of unhygienic macros:
(defmacro yes-no->boolean
[val]
`(let [b (= ~val "yes")]
b))
#'cljs.user/yes-no->boolean
Macroexpansion demonstrates that the Clojure compiler makes the b symbol namespace-qualified
( user is the default namespace in the Clojure REPL). This helps avoid var and local shadowing -- but
let does not allow namespace-qualified symbol so this macro produces invalid code. We'll see how to
avoid this in the next section.
Note: Special forms are not necessarily qualified. See section 'Special Forms in Detail'.
(gensym)
G__59
(gensym "base")
base60
There is a shortcut: if a symbol ends in # within a syntax quote form, it will be expanded by the compiler
into a gensym (also known as an auto-gensym):
(defmacro yes-no->boolean
[val]
`(let [b# (= ~val "yes")]
b#))
#'cljs.user/yes-no->boolean
The name that replaced b# was generated by the compiler to make unwanted variable capture very
unlikely in practice, and impossible if all bindings are named with auto-gensym.
Theoretically, Clojure's approach to generating uncaptured gensyms (incrementing a global counter) can
be circumvented via a mischievous macro or very bad luck.
Tip: Avoid code with __ in local binding names. This ensures auto-gensyms are never captured in
unwanted ways.
Macroexpansions
During macro development, it is important to be able to test the macro and see what data structures the
macro expands to. This can be done with two functions in the core Clojure library, and an additional one
from clojure.walk :
clojure.core/macroexpand-1
clojure.core/macroexpand
clojure.walk/macroexpand-all
The difference between the first two is that macroexpand-1 will expand the macro only once. If the result
contains calls to other macros, those won't be expanded. macroexpand , however, will continue
expanding macros until the top level form is no longer a macro.
Neither macroexpand-1 nor macroexpand expand nested forms. To fully expand macros including those
in nested forms, there is clojure.walk/macroexpand-all which can be useful for debugging macros
but does not behave exactly the same way as the Clojure compiler.
Security Considerations
clojure.core/read-string can execute arbitrary code and must not be used on inputs coming from
untrusted sources. This behavior is controlled by the clojure.core/*read-eval* var, which defaults to
true (unsafe), but can be set to false (safe) via binding .
*read-eval* can also be set via a property when starting the JVM:
-Dclojure.read.eval=false
When reading Clojure forms from untrusted sources, use clojure.edn/read-string , which is does not
perform arbitrary code execution and is safer. clojure.edn/read-string implements the EDN format
(https://github.com/edn-format/edn), a subset of Clojure syntax for data structures. clojure.edn was
introduced in Clojure 1.5.
Special forms must be a list with a special name as the first element.
do
;; Syntax error compiling at (REPL:0:0).
;; Unable to resolve symbol: do in this context
Macros have a similar restriction, but notice: the macro's var is identified in the error while special
names have no meaning at all outside the first element of a list.
dosync
;; Syntax error compiling at (REPL:0:0).
;; Can't take value of a macro: #'clojure.core/dosync
Most special forms (all except clojure.core/import* ) are not namespace qualified. The reader
must circumvent syntax quote's policy of namespace-qualifying all symbols.
`a
cljs.user/a
`do
do
user=> `if
if
user=> `import
import
Ouch!
Note: Be wary of maps with keyword keys with special names, they are more likely to be
destructured this way.
Keep these special cases in mind as you work through the tutorial.
Contributors
Michael Klishin [email protected] (mailto:[email protected]), 2013 (original author)
Ambrose Bonnaire-Sergeant [email protected]
(mailto:[email protected]), 2013