Thanks to Nicola Mometto, Clojure core committer, for contributing this chapter (except “definline”).
Arguably one of the most powerful aspects of any LISP is their ability to define custom and arbitrarily complex macros, and Clojure is no exception. Although many languages have the concept of macros, LISP ones are an entirely different beast, effectively providing the users of the language with an expressive power that in other languages only compiler authors can have.
Given their power one would expect macros to be a complex and advanced feature to use, this is in fact not the case: because of the homoiconic nature of Clojure [81], defining a macro is as simple as defining functions and manipulating data: macros are indeed just regular functions that the compiler invokes at compile time, passing as inputs their arguments as if wrapped in an implicit “quote” invocation and returning a valid Clojure expression that will be evaluated at run time.
Macros can be used for a variety of reasons, from simply reducing the amount of repeated code, to allowing code to be expressed in a more concise way, to writing complex DSLs or embedding small compilers [82].
This chapter is dedicated to the facilities in the standard library (and the language) to create, inspect and help using macros. Here’s a brief summary: