I am learning about Clojure macros, and the code examples will sometimes have the constructs \'~symbol
or alternately ~\'symbol
. I know that (quo
~'symbol
is used to produce an unqualified symbol. Clojure's macros capture namespace by default, so a symbol in a macro would normally be resolved to (your-namespace/symbol)
. The unquote-quote idiom directly results in the simple, unqualified symbol name - (symbol)
- by evaluating to a quoted symbol. From The Joy Of Clojure:
(defmacro awhen [expr & body]
`(let [~'it ~expr] ; refer to the expression as "it" inside the body
(when ~'it
(do ~@body))))
(awhen [:a :b :c] (second it)) ; :b
'~symbol
is likely used to insert a name in the macro or something similar. Here, symbol
will be bound to a value - let [symbol 'my-symbol]
. This value is then inserted into the code the macro produces by evaluating symbol
.
(defmacro def-symbol-print [sym]
`(defn ~(symbol (str "print-" sym)) []
(println '~sym))) ; print the symbol name passed to the macro
(def-symbol-print foo)
(print-foo) ; foo