Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure macros and symbol binding

not sure how to express this..

I've written a macro which takes two arguments. The first one essentially contains identifiers for generating a let expression. The second is the code to use inside the let expression (it wants to have access to these identifiers).

An example:

(match (Add {ast-> x}) (println x))

When the second argument is raw code, things work nicely. x binds to the x defined in the let expression (when macroexpanded it just shows as x). However, when the second argument is a macro which generates (println x), x expands to something like user/x.

Any good ideas on how to fix this?

like image 563
Ellery Newcomer Avatar asked Nov 21 '25 04:11

Ellery Newcomer


1 Answers

It sounds like your second macro is defined as:

(defmacro foo
  []
  `(println x))

This is incorrect as x will be namespace qualified. The correct version of the second macro in this case would be:

(defmacro foo
  []
  `(println ~'x))

Now the x in the println call will be a literal x symbol and not namespace qualified.

like image 123
Brian Avatar answered Nov 24 '25 02:11

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!