emacs.d/clones/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node292.html
2022-08-26 19:11:35 +02:00

105 lines
6.6 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>28.1.9. Object Creation and Initialization</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Object Creation and Initialization">
<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=tex2html5304 HREF="node293.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5302 HREF="node261.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5296 HREF="node291.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5306 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5307 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5305 HREF="node293.html"> Initialization Arguments</A>
<B>Up:</B> <A NAME=tex2html5303 HREF="node261.html"> Programmer Interface Concepts</A>
<B> Previous:</B> <A NAME=tex2html5297 HREF="node291.html"> Standard Meta-objects</A>
<HR> <P>
<H2><A NAME=SECTION003219000000000000000>28.1.9. Object Creation and Initialization</A></H2>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
<A NAME=ObjectCreationandInitializationSECTION>The</A>
generic function <tt>make-instance</tt> creates and returns a new
instance of a class. The first argument is a class or the name of a
class, and the remaining arguments form an <i>initialization argument</i>
list.
<P>
The initialization of a new instance consists of several distinct
steps, including the following: combining the explicitly supplied
initialization arguments with default values for the unsupplied
initialization arguments, checking the validity of the initialization
arguments, allocating storage for the instance, filling slots with
values, and executing user-supplied methods that perform additional
initialization. Each step of <tt>make-instance</tt> is implemented by a
generic function to provide a mechanism for customizing that step. In
addition, <tt>make-instance</tt> is itself a generic function and thus
also can be customized.
<P>
The Object System specifies system-supplied primary methods for each step and
thus specifies a well-defined standard behavior for the entire
initialization process. The standard behavior provides four simple
mechanisms for controlling initialization:
<P>
<UL><LI> Declaring a symbol to be an initialization argument for a
slot. An initialization argument is declared by using the
<tt>:initarg</tt> slot option to <tt>defclass</tt>. This provides a mechanism
for supplying a value for a slot in a call to <tt>make-instance</tt>.
<P>
<LI> Supplying a default value form for an initialization
argument. Default value forms for initialization arguments are
defined by using the <tt>:default-initargs</tt> class option to
<tt>defclass</tt>. If an initialization argument is not explicitly provided
as an argument to <tt>make-instance</tt>, the default value form is
evaluated in the lexical environment of the <tt>defclass</tt> form that
defined it, and the resulting value is used as the value of the
initialization argument.
<P>
<LI> Supplying a default initial value form for a slot. A
default initial value form for a slot is defined by using the
<tt>:initform</tt> slot option to <tt>defclass</tt>. If no initialization
argument associated with that slot is given as an argument to
<tt>make-instance</tt> or is defaulted by <tt>:default-initargs</tt>, this
default initial value form is evaluated in the lexical environment of
the <tt>defclass</tt> form that defined it, and the resulting value is
stored in the slot. The <tt>:initform</tt> form for a local slot may be
used when creating an instance, when updating an instance to conform
to a redefined class, or when updating an instance to conform to the
definition of a different class. The <tt>:initform</tt> form for a shared
slot may be used when defining or re-defining the class.
<P>
<LI> Defining methods for <tt>initialize-instance</tt> and
<tt>shared-initialize</tt>. The slot-filling behavior described above is
implemented by a system-supplied primary method for
<tt>initialize-instance</tt> which invokes <tt>shared-initialize</tt>. The
generic function <tt>shared-initialize</tt> implements the parts of
initialization shared by these four situations: when making an
instance, when re-initializing an instance, when updating an instance
to conform to a redefined class, and when updating an instance to
conform to the definition of a different class. The system-supplied
primary method for <tt>shared-initialize</tt> directly implements the
slot-filling behavior described above, and <tt>initialize-instance</tt>
simply invokes <tt>shared-initialize</tt>.
<P>
</UL>
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<HR>
<UL>
<LI> <A NAME=tex2html5308 HREF="node293.html#SECTION003219100000000000000"> Initialization Arguments</A>
<LI> <A NAME=tex2html5309 HREF="node294.html#SECTION003219200000000000000"> Declaring the Validity of Initialization Arguments</A>
<LI> <A NAME=tex2html5310 HREF="node295.html#SECTION003219300000000000000"> Defaulting of Initialization Arguments</A>
<LI> <A NAME=tex2html5311 HREF="node296.html#SECTION003219400000000000000"> Rules for Initialization Arguments</A>
<LI> <A NAME=tex2html5312 HREF="node297.html#SECTION003219500000000000000"> Shared-Initialize</A>
<LI> <A NAME=tex2html5313 HREF="node298.html#SECTION003219600000000000000"> Initialize-Instance</A>
<LI> <A NAME=tex2html5314 HREF="node299.html#SECTION003219700000000000000"> Definitions of Make-Instance and Initialize-Instance</A>
</UL>
<BR> <HR><A NAME=tex2html5304 HREF="node293.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html5302 HREF="node261.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html5296 HREF="node291.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html5306 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html5307 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html5305 HREF="node293.html"> Initialization Arguments</A>
<B>Up:</B> <A NAME=tex2html5303 HREF="node261.html"> Programmer Interface Concepts</A>
<B> Previous:</B> <A NAME=tex2html5297 HREF="node291.html"> Standard Meta-objects</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>