问题描述
作为一个开始使用 clojure 的非 lisper,我应该如何最好地理解 vars 获得像 *var-name*
这样的名称的命名约定?
As a non-lisper coming to clojure how should I best understand the naming convention where vars get a name like *var-name*
?
这似乎是表示全局变量的 lisp 约定.但在 clojure 中,据我所知,这样的变量出现在命名空间中.
This appears to be a lisp convention indicating a global variable. But in clojure such vars appear in namespaces as far as I can tell.
如果作者在他们的代码中使用此类 var 时我应该期待什么,我真的很感激,最好提供一个示例,说明如何以及为什么在 clojure 库中使用和更改此类 var.
I would really appreciate a brief explanation of what I should expect when an author has used such vars in their code, ideally with a example of how and why such a var would be used and changed in a clojure library.
推荐答案
这是其他 Lisp 中使用的约定,例如 Common Lisp,用于区分 特殊变量,区别于词法变量.special 或 dynamic 变量的绑定存储在动态环境中,这意味着它的当前值对代码中的任何点可见取决于它如何绑定得更高向上调用堆栈,而不是仅依赖于最本地的词法绑定形式(例如 let
或 defn
).
It's a convention used in other Lisps, such as Common Lisp, to distinguish between special variables, as distinct from lexical variables. A special or dynamic variable has its binding stored in a dynamic environment, meaning that its current value as visible to any point in the code depends upon how it may have been bound higher up the call stack, as opposed to being dependent only on the most local lexical binding form (such as let
or defn
).
请注意,在他的著作 Let Over Lambda 中,Doug Hoyte 反对耳罩" 用于命名特殊变量的星号约定.他使用了一种不寻常的宏风格来引用自由变量,并且他不愿意承诺或区分这些符号最终是指向词法变量还是动态变量.
Note that in his book Let Over Lambda, Doug Hoyte argues against the "earmuffs" asterix convention for naming special variables. He uses an unusual macro style that makes reference to free variables, and he prefers not to commit to or distinguish whether those symbols will eventually refer to lexical or dynamic variables.
虽然专门针对 Common Lisp,但您可能会喜欢 Ron Garret 的文章 特殊变量白痴指南.其中大部分仍然适用于 Clojure.
Though targeted specifically at Common Lisp, you might enjoy Ron Garret's essay The Idiot's Guide to Special Variables. Much of it can still apply to Clojure.
这篇关于clojure 中如何使用 `*var-name*` 命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!