113 lines
6.1 KiB
HTML
113 lines
6.1 KiB
HTML
<!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>19.3. Using the Automatically Defined Constructor Function</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Using the Automatically Defined Constructor Function">
|
|
<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=tex2html3666 HREF="node172.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3664 HREF="node168.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3658 HREF="node170.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3668 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3669 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3667 HREF="node172.html"> Defstruct Slot-Options</A>
|
|
<B>Up:</B> <A NAME=tex2html3665 HREF="node168.html"> Structures</A>
|
|
<B> Previous:</B> <A NAME=tex2html3659 HREF="node170.html"> How to Use </A>
|
|
<HR> <P>
|
|
<H1><A NAME=SECTION002330000000000000000>19.3. Using the Automatically Defined Constructor Function</A></H1>
|
|
<P>
|
|
After you have defined a new structure with <tt>defstruct</tt>, you can
|
|
create instances of this structure by using the constructor function.
|
|
By default, <tt>defstruct</tt> defines this function automatically.
|
|
For a structure named <tt>foo</tt>, the constructor function is normally
|
|
named <tt>make-foo</tt>;
|
|
you can specify a different name
|
|
by giving it as the argument to the
|
|
<tt>:constructor</tt> option, or specify that you don't
|
|
want a normal
|
|
constructor function at all by using <tt>nil</tt> as the argument
|
|
(in which case one or more ``by-position'' constructors should be
|
|
requested; see section <A HREF="node174.html#DEFSTRUCTCONSTRUCTORSYNTAX">19.6</A>).
|
|
<P>
|
|
A call to a constructor function, in general, has the form
|
|
<P><pre>
|
|
(<i>name-of-constructor-function</i>
|
|
<i>slot-keyword-1</i> <i>form-1</i>
|
|
<i>slot-keyword-2</i> <i>form-2</i>
|
|
...)
|
|
</pre><P>
|
|
All arguments are keyword arguments. Each <i>slot-keyword</i> should be a
|
|
keyword whose name matches the name of a slot of the structure
|
|
(<tt>defstruct</tt> determines the possible keywords simply by interning each
|
|
slot-name in the keyword package). All the <i>keywords</i> and <i>forms</i>
|
|
are evaluated. In short, it is just as if the constructor function
|
|
took all its arguments as <tt>&key</tt> parameters. For example, the
|
|
<tt>ship</tt> structure shown in section <A HREF="node169.html#DEFSTRUCTINTROSECTION">19.1</A>
|
|
has a constructor function that takes arguments roughly as if its definition
|
|
were
|
|
<P><pre>
|
|
(defun make-ship (&key x-position y-position
|
|
x-velocity y-velocity mass)
|
|
...)
|
|
</pre><P>
|
|
<P>
|
|
<A NAME=defstructinitialization> </A>
|
|
If <i>slot-keyword-j</i> names a slot, then that element of
|
|
the created structure will be initialized to the value of <i>form-j</i>.
|
|
If no pair <i>slot-keyword-j</i> and <i>form-j</i>
|
|
is present for a given slot, then the slot will be
|
|
initialized by evaluating the <i>default-init</i> form specified
|
|
for that slot in the call to <tt>defstruct</tt>.
|
|
(In other words, the initialization specified in the <tt>defstruct</tt>
|
|
defers to any specified in a call to the constructor function.)
|
|
If the default initialization form is used, it is evaluated
|
|
at construction time, but
|
|
in the lexical environment of the <tt>defstruct</tt> form in which it appeared.
|
|
If the <tt>defstruct</tt> itself also did not
|
|
specify any initialization, the element's initial value is undefined.
|
|
You should always specify the initialization, either in the <tt>defstruct</tt>
|
|
or in the call to the constructor function,
|
|
if you care about the initial value of the slot.
|
|
<P>
|
|
Each initialization form specified for a <tt>defstruct</tt> component,
|
|
when used by the constructor function for an otherwise unspecified
|
|
component, is re-evaluated on every call to the
|
|
constructor function. It is as if the initialization forms were
|
|
used as <i>init</i> forms for the keyword parameters of the
|
|
constructor function.
|
|
For example, if the form <tt>(gensym)</tt>
|
|
were used as an initialization form,
|
|
either in the constructor-function call or as the default initialization form
|
|
in the <tt>defstruct</tt> form,
|
|
then every call to the constructor
|
|
function would call <tt>gensym</tt> once to generate a new symbol.
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
X3J13 voted in October 1988 (DEFSTRUCT-DEFAULT-VALUE-EVALUATION) <A NAME=18641> </A>
|
|
to clarify that the default value in a defstruct slot is not evaluated
|
|
unless it is needed in the creation of a particular structure
|
|
instance. If it is never needed, there can be no type-mismatch
|
|
error, even if the type of the slot is specified, and no warning
|
|
should be issued.
|
|
<P>
|
|
For example, in the following sequence only the last form is in error.
|
|
<P><pre>
|
|
(defstruct person (name .007 :type string))
|
|
|
|
(make-person :name "James")
|
|
|
|
(make-person) ;Error to give <tt>name</tt> the value <tt>.007</tt>
|
|
</pre><P>
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3666 HREF="node172.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3664 HREF="node168.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3658 HREF="node170.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3668 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3669 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3667 HREF="node172.html"> Defstruct Slot-Options</A>
|
|
<B>Up:</B> <A NAME=tex2html3665 HREF="node168.html"> Structures</A>
|
|
<B> Previous:</B> <A NAME=tex2html3659 HREF="node170.html"> How to Use </A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|