Inconsistency in Clojure: functions in macros and IllegalArgumentException

前端 未结 2 917
广开言路
广开言路 2020-12-30 08:06

Two following examples of using a function in a macro result in evaluations without errors.

(defmacro works []
  (let [f (fn [] 1)]
    `(~f)))
(works)
;; =&         


        
2条回答
  •  伪装坚强ぢ
    2020-12-30 08:32

    See this example that does also throw the exception:

    (defmacro does-also-not-work []
      (let [x 4
            f (fn [] x)]
        `(~f)))
    

    Just like the result of constantly, but unlike your first two examples, f here is a closure. Apparently, closures created during macro-expansion time do not persist.

提交回复
热议问题