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

后端 未结 2 1515
情深已故
情深已故 2021-02-01 17:35

I am learning about Clojure macros, and the code examples will sometimes have the constructs \'~symbol or alternately ~\'symbol. I know that (quo

2条回答
  •  时光取名叫无心
    2021-02-01 18:09

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

提交回复
热议问题