1
0
Fork 0
cl-sites/novaspec.org/cl/f_defparameter.html
2025-02-05 18:52:26 +01:00

713 lines
No EOL
16 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>defparameter, defvar | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_defparameter.html"
><LINK REL="next" HREF="f_destructuring-bind.html" TYPE="text/html" TITLE="destructuring-bind"
><LINK REL="prev" HREF="f_defconstant.html" TYPE="text/html" TITLE="defconstant"
><LINK REL="up" HREF="5_3_Data_and_Control_Flow_Dictionary.html" TYPE="text/html" TITLE="5.3 Data and Control Flow Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="5_Data_and_Control_Flow.html"
>5. Data and Control Flow</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="5_3_Data_and_Control_Flow_Dictionary.html"
>5.3 Data and Control Flow Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_defparameter.html"
>defparameter, defvar</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_defconstant.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_destructuring-bind.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="defparameter, defvar"
></SPAN
><B
>defparameter, defvar</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Macro</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>defparameter</B
> <SPAN CLASS="cmssi"
>name</SPAN
> <SPAN CLASS="cmssi"
>initial-value</SPAN
> [<SPAN CLASS="cmssi"
>documentation</SPAN
>] <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>name</SPAN
></P
><P CLASS="j"
><B
>defvar</B
> <SPAN CLASS="cmssi"
>name</SPAN
> <SPAN CLASS="cmtt"
>[</SPAN
><SPAN CLASS="cmssi"
>initial-value</SPAN
> [<SPAN CLASS="cmssi"
>documentation</SPAN
>]<SPAN CLASS="cmtt"
>]</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>name</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>name</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>initial-value</VAR
> &#8212; a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>; for <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
>, it is always <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluated</EM
></A
>, but for <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> it is <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluated</EM
></A
> only if <VAR CLASS="param"
>name</VAR
> is not already <A HREF="26_1_Glossary.html#bound"
><EM CLASS="term"
>bound</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>documentation</VAR
> &#8212; a <VAR CLASS="param"
>string</VAR
>; not evaluated.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> and <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> <A HREF="26_1_Glossary.html#establish"
><EM CLASS="term"
>establish</EM
></A
> <VAR CLASS="param"
>name</VAR
> as a <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
>. </P
><P CLASS="j"
><A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> unconditionally <A HREF="26_1_Glossary.html#assign"
><EM CLASS="term"
>assigns</EM
></A
> the <VAR CLASS="param"
>initial-value</VAR
> to the <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
> named <VAR CLASS="param"
>name</VAR
>. <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
>, by contrast, <A HREF="26_1_Glossary.html#assign"
><EM CLASS="term"
>assigns</EM
></A
> <VAR CLASS="param"
>initial-value</VAR
> (if supplied) to the <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
> named <VAR CLASS="param"
>name</VAR
> only if <VAR CLASS="param"
>name</VAR
> is not already <A HREF="26_1_Glossary.html#bound"
><EM CLASS="term"
>bound</EM
></A
>. </P
><P CLASS="j"
>If no <VAR CLASS="param"
>initial-value</VAR
> is supplied, <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> leaves the <A HREF="26_1_Glossary.html#value_cell"
><EM CLASS="term"
>value cell</EM
></A
> of the <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
> named <VAR CLASS="param"
>name</VAR
> undisturbed; if <VAR CLASS="param"
>name</VAR
> was previously <A HREF="26_1_Glossary.html#bound"
><EM CLASS="term"
>bound</EM
></A
>, its old <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>value</EM
></A
> persists, and if it was previously <A HREF="26_1_Glossary.html#unbound"
><EM CLASS="term"
>unbound</EM
></A
>, it remains <A HREF="26_1_Glossary.html#unbound"
><EM CLASS="term"
>unbound</EM
></A
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>documentation</VAR
> is supplied, it is attached to <VAR CLASS="param"
>name</VAR
> as a <A HREF="26_1_Glossary.html#documentation_string"
><EM CLASS="term"
>documentation string</EM
></A
> of kind <SPAN CLASS="misc"
><B
>variable</B
></SPAN
>. </P
><P CLASS="j"
><A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> and <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> normally appear as a <A HREF="26_1_Glossary.html#top_level_form"
><EM CLASS="term"
>top level form</EM
></A
>, but it is meaningful for them to appear as <A HREF="26_1_Glossary.html#non-top-level_form"
><EM CLASS="term"
>non-top-level forms</EM
></A
>. However, the compile-time side effects described below only take place when they appear as <A HREF="26_1_Glossary.html#top_level_form"
><EM CLASS="term"
>top level forms</EM
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(defparameter *p* 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *P*
*p* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(constantp '*p*) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(setq *p* 2) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(defparameter *p* 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *P*
*p* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(defvar *v* 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *V*
*v* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(constantp '*v*) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(setq *v* 2) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(defvar *v* 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *V*
*v* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(defun foo ()
(let ((*p* 'p) (*v* 'v))
(bar))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> FOO
(defun bar () (list *p* *v*)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> BAR
(foo) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (P V)</PRE
><P CLASS="j"
>The principal operational distinction between <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> and <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> is that <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> makes an unconditional assignment to <VAR CLASS="param"
>name</VAR
>, while <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> makes a conditional one. In practice, this means that <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> is useful in situations where loading or reloading the definition would want to pick up a new value of the variable, while <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> is used in situations where the old value would want to be retained if the file were loaded or reloaded. For example, one might create a file which contained:</P
><PRE CLASS="screen"
>(defvar *the-interesting-numbers* '())
(defmacro define-interesting-number (name n)
`(progn (defvar ,name ,n)
(pushnew ,name *the-interesting-numbers*)
',name))
(define-interesting-number *my-height* 168) ;cm
(define-interesting-number *my-weight* 13) ;stones</PRE
><P CLASS="j"
>Here the initial value, <CODE CLASS="f"
>()</CODE
>, for the variable <CODE CLASS="f"
>*the-interesting-numbers*</CODE
> is just a seed that we are never likely to want to reset to something else once something has been grown from it. As such, we have used <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> to avoid having the <CODE CLASS="f"
>*interesting-numbers*</CODE
> information reset if the file is loaded a second time. It is true that the two calls to <SPAN CLASS="macref"
><B
>define-interesting-number</B
></SPAN
> here would be reprocessed, but if there were additional calls in another file, they would not be and that information would be lost. On the other hand, consider the following code:</P
><PRE CLASS="screen"
>(defparameter *default-beep-count* 3)
(defun beep (&amp;optional (n *default-beep-count*))
(dotimes (i n) (si:%beep 1000. 100000.) (sleep 0.1)))</PRE
><P CLASS="j"
>Here we could easily imagine editing the code to change the initial value of <CODE CLASS="f"
>*default-beep-count*</CODE
>, and then reloading the file to pick up the new value. In order to make value updating easy, we have used <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
>. </P
><P CLASS="j"
>On the other hand, there is potential value to using <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> in this situation. For example, suppose that someone had predefined an alternate value for <CODE CLASS="f"
>*default-beep-count*</CODE
>, or had loaded the file and then manually changed the value. In both cases, if we had used <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> instead of <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
>, those user preferences would not be overridden by (re)loading the file. </P
><P CLASS="j"
>The choice of whether to use <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> or <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> has visible consequences to programs, but is nevertheless often made for subjective reasons.</P
></DD
><DT
><B
>Side Effects</B
></DT
><DD
><P CLASS="j"
>If a <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> or <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> appears as a <A HREF="26_1_Glossary.html#top_level_form"
><EM CLASS="term"
>top level form</EM
></A
>, the <A HREF="26_1_Glossary.html#compiler"
><EM CLASS="term"
>compiler</EM
></A
> must recognize that the <VAR CLASS="param"
>name</VAR
> has been proclaimed <A HREF="m_special.html" CLASS="declref"
><B
>special</B
></A
>. However, it must neither <A HREF="26_1_Glossary.html#evaluate"
><EM CLASS="term"
>evaluate</EM
></A
> the <VAR CLASS="param"
>initial-value</VAR
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> nor <A HREF="26_1_Glossary.html#assign"
><EM CLASS="term"
>assign</EM
></A
> the <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
> named <VAR CLASS="param"
>name</VAR
> at compile time. </P
><P CLASS="j"
>There may be additional (<A HREF="26_1_Glossary.html#implementation-defined"
><EM CLASS="term"
>implementation-defined</EM
></A
>) compile-time or run-time side effects, as long as such effects do not interfere with the correct operation of <A HREF="26_1_Glossary.html#conforming_program"
><EM CLASS="term"
>conforming programs</EM
></A
>.</P
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
><A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> is affected by whether <VAR CLASS="param"
>name</VAR
> is already <A HREF="26_1_Glossary.html#bound"
><EM CLASS="term"
>bound</EM
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_declaim.html" CLASS="macref"
><B
>declaim</B
></A
>, <A HREF="f_defconstant.html" CLASS="macref"
><B
>defconstant</B
></A
>, <A HREF="f_documentation.html" CLASS="funref"
><B
>documentation</B
></A
>, <A HREF="3_2_Compilation.html#sec_3_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.2</SPAN
> <SPAN CLASS="cmr"
>(Compilation)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>It is customary to name <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variables</EM
></A
> with an <A HREF="26_1_Glossary.html#asterisk"
><EM CLASS="term"
>asterisk</EM
></A
> at the beginning and end of the name. e.g., <CODE CLASS="f"
>*foo*</CODE
> is a good name for a <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
>, but not for a <A HREF="26_1_Glossary.html#lexical_variable"
><EM CLASS="term"
>lexical variable</EM
></A
>; <CODE CLASS="f"
>foo</CODE
> is a good name for a <A HREF="26_1_Glossary.html#lexical_variable"
><EM CLASS="term"
>lexical variable</EM
></A
>, but not for a <A HREF="26_1_Glossary.html#dynamic_variable"
><EM CLASS="term"
>dynamic variable</EM
></A
>. This naming convention is observed for all <A HREF="26_1_Glossary.html#defined_name"
><EM CLASS="term"
>defined names</EM
></A
> in Common Lisp; however, neither <A HREF="26_1_Glossary.html#conforming_program"
><EM CLASS="term"
>conforming programs</EM
></A
> nor <A HREF="26_1_Glossary.html#conforming_implementation"
><EM CLASS="term"
>conforming implementations</EM
></A
> are obliged to adhere to this convention. </P
><P CLASS="j"
>The intent of the permission for additional side effects is to allow <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementations</EM
></A
> to do normal &#8220;bookkeeping&#8221; that accompanies definitions. For example, the <A HREF="26_1_Glossary.html#macro_expansion"
><EM CLASS="term"
>macro expansion</EM
></A
> of a <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> or <A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> might include code that arranges to record the name of the source file in which the definition occurs. </P
><P CLASS="j"
><A HREF="f_defparameter.html" CLASS="macref"
><B
>defparameter</B
></A
> and <A HREF="f_defparameter.html" CLASS="macref"
><B
>defvar</B
></A
> might be defined as follows:</P
><PRE CLASS="screen"
>(defmacro defparameter (name initial-value
&amp;optional (documentation nil documentation-p))
`(progn (declaim (special ,name))
(setf (symbol-value ',name) ,initial-value)
,(when documentation-p
`(setf (documentation ',name 'variable) ',documentation))
',name))
(defmacro defvar (name &amp;optional
(initial-value nil initial-value-p)
(documentation nil documentation-p))
`(progn (declaim (special ,name))
,(when initial-value-p
`(unless (boundp ',name)
(setf (symbol-value ',name) ,initial-value)))
,(when documentation-p
`(setf (documentation ',name 'variable) ',documentation))
',name))</PRE
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_defconstant.html" CLASS="prev"
>&#8592;</A
><A HREF="f_destructuring-bind.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>