Книга: Practical Common Lisp
Constants
Constants
One other kind of variable I haven't mentioned at all is the oxymoronic "constant variable." All constants are global and are defined with DEFCONSTANT
. The basic form of DEFCONSTANT
is like DEFPARAMETER
.
(defconstant name initial-value-form [ documentation-string ])
As with DEFVAR
and DEFPARAMETER
, DEFCONSTANT
has a global effect on the name used—thereafter the name can be used only to refer to the constant; it can't be used as a function parameter or rebound with any other binding form. Thus, many Lisp programmers follow a naming convention of using names starting and ending with +
for constants. This convention is somewhat less universally followed than the *
-naming convention for globally special names but is a good idea for the same reason.[80]
Another thing to note about DEFCONSTANT
is that while the language allows you to redefine a constant by reevaluating a DEFCONSTANT
with a different initial-value-form, what exactly happens after the redefinition isn't defined. In practice, most implementations will require you to reevaluate any code that refers to the constant in order to see the new value since the old value may well have been inlined. Consequently, it's a good idea to use DEFCONSTANT
only to define things that are really constant, such as the value of NIL. For things you might ever want to change, you should use DEFPARAMETER
instead.