Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure var-defining macro

Tags:

macros

clojure

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 ?

like image 783
Dfr Avatar asked Dec 04 '12 17:12

Dfr


1 Answers

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

like image 117
Arthur Ulfeldt Avatar answered Oct 01 '22 17:10

Arthur Ulfeldt