开发者

How is the `*var-name*` naming-convention used in clojure?

开发者 https://www.devze.com 2022-12-15 08:59 出处:网络
As a non-lisper coming to clojure how should I best understand the naming convention where vars get a name like *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*?

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.

I would really appreciate a brief explanation of what I should expect when an 开发者_StackOverflow中文版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.


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).

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.

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.


Functional programming is all about safe predictable functions. Infact some of us are afraid of that spooky "action at a distance" thing. When people call a function they get a warm fuzzy satisfaction that the function will always give them the same result if they call the function or read the value again. the *un-warm-and-fuzzy* bristly things exist to warn programmers that this variable is less cuddly than some of the others.


Some references I found in the Clojure newsgroups:

Re: making code readable John D. Hume Tue, 30 Dec 2008 08:30:57 -0800

On Mon, Dec 29, 2008 at 4:10 PM, Chouser wrote: I believe the idiom for global values like this is to place asterisks around the name.

I thought the asterisk convention was for variables intended for dynamic binding. It took me a minute to figure out where I got that idea. "Programming Clojure" suggests it (without quite saying it) in chapter 6, section 3.

"Vars intended for dynamic binding are sometimes called special vari-
ables. It is good style to name them with leading and trailing asterisks."

Obviously the book's a work in progress, but that does sound reasonable. A special convention for variables whose values change (or that my code's welcome to rebind) seems more useful to me than one for "globals" (though I'm not sure I'd consider something like grid-size for a given application a global). Based on ants.clj it appears Rich doesn't feel there needs to be a special naming convention for that sort of value.

and...

I believe the idiom for global values like this is to place asterisks around the name. Underscores (and CamelCase) should only be used when required for Java interop:

(def *grid-size* 10)
(def *height* 600)
(def *margin* 50)
(def *x-index* 0)
(def *y-index* 1)
0

精彩评论

暂无评论...
验证码 换一张
取 消