Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure when macro

Tags:

clojure

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?

like image 989
Kevin Avatar asked Aug 03 '12 22:08

Kevin


People also ask

Do clojure do macros?

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.

What is a macro in Elixir?

Macros are compile-time constructs that are invoked with Elixir's AST as input and a superset of Elixir's AST as output.


1 Answers

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

like image 145
Arthur Ulfeldt Avatar answered Sep 18 '22 17:09

Arthur Ulfeldt