我一直在看def宏的源代码,它在其定义中使用了"let“:
(def
^{:doc "Like defn, but the resulting function name is declared as a
macro and will be used as a macro by the compiler when it is
called."
:arglists '([name doc-string? attr-map? [params*] body]
[name doc-string? attr-map? ([params*] body)+ attr-map?])
:added "1.0"}
defmacro (fn [&form &env
name & args]
(let [prefix (loop [p (list name) args args]然而,"let“被定义为宏本身:
(defmacro let
"binding => binding-form init-expr
Evaluates the exprs in a lexical context in which the symbols in
the binding-forms are bound to their respective init-exprs or parts
therein."
{:added "1.0", :special-form true, :forms '[(let [bindings*] exprs*)]}
[bindings & body]
(assert-args
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings) ~@body))有人能解释一下这是如何工作的吗,因为我不能理解为什么“def宏”可以被定义为需要“def宏”已经被定义的东西。(如果这是有意义的:)
发布于 2012-08-31 12:43:46
这是可能的,因为在core.clj中定义defmacro函数之前,this location中已经有了let的定义(稍后会重新定义)。宏只是普通函数,它们绑定到的变量具有值为true的元数据键:macro,以便编译器在编译时可以区分宏(在编译时执行)和函数,没有这个元键就无法区分宏和函数,因为宏本身就是一个恰好处理S表达式的函数。
https://stackoverflow.com/questions/12207008
复制相似问题