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?
Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.
Macros are compile-time constructs that are invoked with Elixir's AST as input and a superset of Elixir's AST as output.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With