Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure.core: . operator, defmacro and setMacro

In looking through the clojure.core file after the defmacro definition you come to the following code:

(. (var defmacro) (setMacro))

What does this mean and do?

like image 455
Alistair Collins Avatar asked Jan 19 '23 05:01

Alistair Collins


1 Answers

The meaning of . is as explained by Joost's and Jeremy's answers. In case you're wondering about the details of what is accomplished by this particular method call:

This basically inserts a key called :macro with the value true into the metadata map of the Var whose name is clojure.core/defmacro. The effect of this is that from this point on, if the compiler encounters the symbol defmacro in a context where it resolves to the var #'clojure.core/defmacro (#'foo is shorthand for (var foo)) -- which will normally be everywhere, though you could e.g. shadow this Var with a let-bound local variable -- it will know that it should treat it as appropriate in the case of a name of a macro.

(Which is to say, either (1) expand the macro call using the function bound to #'clojure.core/defmacro if the symbol defmacro occurs in the operator position, i.e. immediately to the right of an opening paren, or else throw an exception to complain about a macro name being mentioned in non-operator position.)

Incidentally, the Clojure compiler routinely makes this sort of use of metadata maps on Vars, e.g. to decide on the types of various objects (cf. :tag metadata) or whether to inline a function call (IIRC, :inline and :inline-arities).

like image 168
Michał Marczyk Avatar answered Jan 25 '23 13:01

Michał Marczyk