1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node67.html

175 lines
8.9 KiB
HTML
Raw Normal View History

2023-10-25 11:23:21 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>5.3.2. Declaring Global Variables and Named Constants</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Declaring Global Variables and Named Constants">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html2346 HREF="node68.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2344 HREF="node65.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2338 HREF="node66.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2348 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2349 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2347 HREF="node68.html"> Control of Time </A>
<B>Up:</B> <A NAME=tex2html2345 HREF="node65.html"> Top-Level Forms</A>
<B> Previous:</B> <A NAME=tex2html2339 HREF="node66.html"> Defining Named Functions</A>
<HR> <P>
<H2><A NAME=SECTION00932000000000000000>5.3.2. Declaring Global Variables and Named Constants</A></H2>
<P>
The <tt>defvar</tt> and <tt>defparameter</tt> special forms are
the usual means of specifying globally defined variables.
The <tt>defconstant</tt> special form is used for defining named constants.
<P>
<BR><b>[Macro]</b><BR>
<tt>defvar</tt> <tt><i>name</I></tt> <tt>[<i>initial-value</I></tt> <tt>[<i>documentation</I>]]</tt> <tt><BR>
</tt><tt>defparameter</tt> <tt><i>name</I></tt> <tt><i>initial-value</i></tt> <tt>[<i>documentation</i>]</tt> <tt><BR>
</tt><tt>defconstant</tt> <tt><i>name</i></tt> <tt><i>initial-value</i></tt> <tt>[<i>documentation</i>]</tt><P>
<tt>defvar</tt> is the recommended way to declare the use
of a special variable in a program.
<P><pre>
(defvar <i>variable</i>)
</pre><P>
proclaims <i>variable</i> to be <tt>special</tt> (see <tt>proclaim</tt>),
and may perform other system-dependent bookkeeping actions.
<p>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1987 (DEFVAR-INITIALIZATION) <A NAME=3592>&#160;</A> to clarify
that if no <i>initial-value</i> form is provided, <tt>defvar</tt>
does not change the value of the <i>variable</i>;
if no <i>initial-value</i> form is provided and the variable
has no value, <tt>defvar</tt> does not give it a value.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
If a second argument form is supplied,
<P><pre>
(defvar <i>variable</i> <i>initial-value</i>)
</pre><P>
then <i>variable</i> is initialized to the result of evaluating the form
<i>initial-value</i> unless it already has a value. The <i>initial-value</i> form
is not evaluated unless it is used; this fact is useful if
evaluation of the <i>initial-value</i> form does something
expensive like creating a large data structure.
<p>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1987 (DEFVAR-INIT-TIME) <A NAME=3608>&#160;</A> to clarify that
evaluation of the <i>initial-value</i> and the initialization of the
variable occur, if at all, at the time the <tt>defvar</tt> form is executed,
and that the <i>initial-value</i> form is evaluated
if and only if the <i>variable</i> does not already have a value.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
The initialization is
performed by assignment and thus assigns a global value to the variable
unless there are currently special bindings of that variable.
Normally there should not be any such special bindings.
<P>
<tt>defvar</tt> also provides a good place to put a comment describing the
meaning of the variable, whereas an ordinary <tt>special</tt> proclamation
offers the
temptation to declare several variables at once and not have room to
describe them all.
<P><pre>
(defvar *visible-windows* 0
&quot;Number of windows at least partially visible on the screen&quot;)
</pre><P>
<P>
<tt>defparameter</tt> is similar to <tt>defvar</tt>, but <tt>defparameter</tt> requires
an <i>initial-value</i> form, always evaluates the form, and assigns the
result to the variable. The semantic distinction is that <tt>defvar</tt>
is intended to declare a variable changed by the program, whereas
<tt>defparameter</tt> is intended to declare a variable that is normally
constant but can be changed (possibly at run time), where such a change
is considered a
change <i>to</i> the program. <tt>defparameter</tt> therefore does not indicate
that the quantity <i>never</i> changes; in particular, it does not license
the compiler to build assumptions about the value into programs being
compiled.
<P>
<tt>defconstant</tt> is like <tt>defparameter</tt> but <i>does</i> assert that
the value of the variable <i>name</i> is fixed and does license
the compiler to build assumptions about the value into programs being
compiled. (However, if the compiler chooses to replace references
to the name of the constant by the value of the constant in code
to be compiled, perhaps in order to allow further optimization,
the compiler must take care that such ``copies'' appear to be <tt>eql</tt>
to the object that is the actual value of the constant. For example,
the compiler may freely make copies of numbers but must exercise
care when the value is a list.)
<P>
It is an error if there are any special bindings
of the variable at the time the <tt>defconstant</tt> form
is executed (but implementations may or may not check for this).
<P>
Once a name has been declared by <tt>defconstant</tt> to be constant,
any further assignment to or binding of that special variable is an error.
This is the case for such system-supplied constants as <tt>t</tt> and
<tt>most-positive-fixnum</tt>.
A compiler may also choose to issue warnings about bindings of
the lexical variable of the same name.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in January 1989
(DEFCONSTANT-SPECIAL) <A NAME=3637>&#160;</A>
to clarify the preceding paragraph by specifying
that it is an error to rebind constant symbols
as either lexical or special variables.
Consequently, a valid reference to a symbol declared with <tt>defconstant</tt>
always refers to its global value.
(Unfortunately, this violates the principle of referential transparency,
for one cannot always choose names for lexical variables without regard
to surrounding context.)
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
For any of these constructs,
the documentation should be a string. The string is attached
to the name of the variable, parameter, or constant
under the <tt>variable</tt> documentation type; see the <tt>documentation</tt>
function.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in March 1988
(DEFVAR-DOCUMENTATION) <A NAME=3643>&#160;</A>
to clarify that the <i>documentation-string</i>
is not evaluated but must appear as a literal string when the <tt>defvar</tt>,
<tt>defparameter</tt>, or <tt>defconstant</tt> form is evaluated.
<P>
For example,
the form
<P><pre>
(defvar *avoid-registers* nil &quot;Compilation control switch #43&quot;)
</pre><P>
is legitimate, but
<P><pre>
(defvar *avoid-registers* nil
(format nil &quot;Compilation control switch #~D&quot;
(incf *compiler-switch-number*)))
</pre><P>
is erroneous because the call to <tt>format</tt> is not a literal string.
<P>
(On the other hand, the form
<P><pre>
(defvar *avoid-registers* nil
#.(format nil &quot;Compilation control switch #~D&quot;
(incf *compiler-switch-number*)))
</pre><P>
might be used to accomplish the same purpose, because the call to
<tt>format</tt> is evaluated at <tt>read</tt> time; when the <tt>defvar</tt> form
is evaluated, only the result of the call to <tt>format</tt>, a string,
appears in the <tt>defvar</tt> form.)
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
These constructs are normally used only as top-level forms. The
value returned by each of these constructs is the <i>name</i> declared.
<P>
<BR> <HR><A NAME=tex2html2346 HREF="node68.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2344 HREF="node65.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2338 HREF="node66.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2348 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2349 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2347 HREF="node68.html"> Control of Time </A>
<B>Up:</B> <A NAME=tex2html2345 HREF="node65.html"> Top-Level Forms</A>
<B> Previous:</B> <A NAME=tex2html2339 HREF="node66.html"> Defining Named Functions</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>