32
votes

I was browsing the clojure source and I was surprised by the way the when macro is defined:

user=> (source when)
(defmacro when
  "Evaluates test. If logical true, evaluates body in an implicit do."
  {:added "1.0"}
  [test & body]
  (list 'if test (cons 'do body)))
nil
user=>

I was expecting it to be written something like this instead:

(defmacro when [test & body] `(if ~test (do ~@body)))

Why was the actual macro written in this less usual way?

1

1 Answers

68
votes

core.clj is built from top to bottom, starting with just what Java provides and building up all the requirements for Clojure as it goes. When when is defined the syntax quote does not yet exist.
The when macro is defined on line 456 of core.clj and the requirements for syntax-quote are not available until line 682. the when macro is used to define syntax quoting