首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在clojure中,如何根据定义宏本身来定义呢?

在clojure中,如何根据定义宏本身来定义呢?
EN

Stack Overflow用户
提问于 2012-08-31 07:26:05
回答 1查看 289关注 0票数 7

我一直在看def宏的源代码,它在其定义中使用了"let“:

代码语言:javascript
复制
(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“被定义为宏本身:

代码语言:javascript
复制
(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宏”已经被定义的东西。(如果这是有意义的:)

EN

回答 1

Stack Overflow用户

发布于 2012-08-31 12:43:46

这是可能的,因为在core.clj中定义defmacro函数之前,this location中已经有了let的定义(稍后会重新定义)。宏只是普通函数,它们绑定到的变量具有值为true的元数据键:macro,以便编译器在编译时可以区分宏(在编译时执行)和函数,没有这个元键就无法区分宏和函数,因为宏本身就是一个恰好处理S表达式的函数。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12207008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档