I just learning macros and clojure macros in particular and i'm curious is it possible to do something like this:
(defmacro with-a=hello [f]
`(let [a "hello"] ~f))
(with-a=hello (println a))
This not works for me and throws error: CompilerException java.lang.RuntimeException: Can't let qualified name: user/a, compiling:(NO_SOURCE_PATH:1)
As i undelstand for now, scheme's define-syntax allow to do something like this, but is there clojure way for this ?
By default the syntax-quote form ` prevents introducing un-namespaced symbols and symbol capture in macros. When you intentionally do this you can use the sequence ~'
to introduce an unqualified symbol into a macro.
(defmacro with-a=hello [f]
`(let [~'a "hello"] ~f))
user> (with-a=hello (println a))
hello
nil
macros that do this have the fancy name anaphoric macros
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