32

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?

0

1 Answer 1

68

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

Sign up to request clarification or add additional context in comments.

3 Comments

Line 680 appears to be the defmulti macro. And syntax quote (I assume) is a reader macro. Can you elaborate on exactly what function/macro is required for syntax quote?
Kevin, why don't you just follow the links behind those line numbers. There is no defmulti at or near line 682 so you're probably looking at some other version of this file. As to what is required for syntax quote, it's simple: everything defined above line 682 in this particular version of the file.
@marco topolnok: I did follow your link. Just realized the line numbers get screwed up on the iPhone. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.