Search code examples
clojuremacros

What is the purpose of ~' or '~ in Clojure?


I am learning about Clojure macros, and the code examples will sometimes have the constructs '~symbol or alternately ~'symbol. I know that (quote and ' prevent a form from being evaluated, and that the backquote additionally adds namespace qualification, and that ~ causes a quoted form to be evaluated. My question is: why is it useful to stop then start evaluation? I also assume that ~'symbol and '~symbol are different, but how so?


Solution

  • ~'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