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

前端 未结 3 501
死守一世寂寞
死守一世寂寞 2020-12-29 03:59

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 co

3条回答
  •  遥遥无期
    2020-12-29 04:19

    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)
    

提交回复
热议问题