我在 Clojure 的源代码中浏览 core.clj 并发现了 defn
的定义。它定义的函数需要几个参数,第一个参数是 &form
和 &env
:
(def
defn (fn defn [&form &env name & fdecl]
;; ...
))
这些名称是否因为以 &
开头而具有特殊含义?
我知道剩余参数和速记匿名函数 var %&
,但这似乎不是这些事情中的任何一个。
我在 Reader reference 中找不到任何有关此语法的信息。 ,其中没有明确提及 &
:
Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually).
Reading Clojure Characters似乎也没有提到这一点。 resolve
在其文档字符串中包含一个 &env
arg,但没有详细说明。
最佳答案
符号&form
和&env
表示Clojure宏中使用的特殊变量:https://clojure.org/reference/macros#_special_variables .
特别是,您可以使用 &form
变量来访问正在计算宏的行号
(:line (meta &form))
这在打印错误消息等方面很有用。
<小时/>关于搜索符号较多的术语的旁注:
SymbolHound.com 是一个不会像 Google 那样删除符号的搜索引擎,因此您通常可以在那里找到其他地方找不到的结果。搜索 clojure &form
yields results .
更新
查看the source code我们看到在 Clojure 引导过程中,fn
是一个使用 fn*
的宏:
(def
^{:macro true
:added "1.0"}
fn (fn* fn [&form &env & decl]
(.withMeta ^clojure.lang.IObj (cons 'fn* decl)
(.meta ^clojure.lang.IMeta &form))))
这就是编译器注入(inject) &form
等的方式
关于syntax - 以 & 开头的 Clojure 符号是否有特殊含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295466/