当宏观的时候

Kev*_*vin 32 clojure

我正在浏览clojure源,我对定义when宏的方式感到惊讶:

user=> (source when)
(defmacro when
  "Evaluates test. If logical true, evaluates body in an implicit do."
  {:added "1.0"}
  [test & body]
  (list 'if test (cons 'do body)))
nil
user=>
Run Code Online (Sandbox Code Playgroud)

我期待它写成这样的东西:

(defmacro when [test & body] `(if ~test (do ~@body)))
Run Code Online (Sandbox Code Playgroud)

为什么用这种不太常见的方式写出实际的宏?

Art*_*ldt 68

core.clj是从上到下构建的,从Java提供的内容开始,并构建了Clojure的所有要求.何时when定义语法引用尚不存在.
when宏被限定在456线 core.clj和用于语法引号的要求并不可用,直到线682 的当宏用于定义语法引用