Next: , Previous: , Up: Variables   [Contents][Index]

5.8.5 Defining Variables

The special forms define, defvar and defconst allow you to define the global variables that will be used by a program.

Macro: define variable form

Defines a lexically scoped global variable called variable. It will have the result of evaluating form assigned to it.

Note that the define special form may also be used to declare block-structured functions, Definitions.

Special Form: defvar variable [form [doc-string]]

This special form defines a special (i.e. dynamically scoped) variable, the symbol variable. If the value of variable is void the form is evaluated and its value is stored as the value of variable (note that only the default value is modified, never a buffer-local value). If no form is given the assigned value defaults to false.

If the doc-string argument is defined it is a string documenting variable (see Docstrings).

(defvar *my-variable* '(x y)
  "This variable is an example showing the usage of the defvar
special form.")
    ⇒ *my-variable*

Don’t use defvar if define suffices. For the reason, see See Modules and Special Variables.

Macro: defvar-setq var #!optional value doc

It’s defvar + setq, so defines var as a special variable, and sets the value to value.

Macro: define-special-variable var #!optional value doc

Same as defvar-setq above. A very confusing name, so it’s deprecated now.

Macro: defconst constant form [doc-string]

defconst defines a global constant, the symbol constant. Its value is set to the result of evaluating form. Note that unlike defvar the value of the symbol is always set, even if it already has a value.

The doc-string argument, if defined, is the documentation string for the constant.

(defconst the-answer 42
  "An example constant.")
    ⇒ the-answer

Next: , Previous: , Up: Variables   [Contents][Index]