58 lines
9.4 KiB
HTML
58 lines
9.4 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Section 7.1</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="07_.htm">
|
|
<LINK REL=UP HREF="07_.htm">
|
|
<LINK REL=NEXT HREF="07_aa.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="07_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="07_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="07_aa.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<H2>
|
|
7.1 Object Creation and Initialization</H2> <P>
|
|
The <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A> creates and returns a new <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> of a <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>. The first argument is a <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A> or the <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A> of a <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>, and the remaining arguments form an <A REL=DEFINITION HREF="26_glo_i.htm#initialization_argument_list"><I>initialization argument list</I></A>. <P>
|
|
The initialization of a new <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> 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 <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A>, filling <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slots</I></A> with values, and executing user-supplied <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A> that perform additional initialization. Each step of <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A> is implemented by a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> to provide a mechanism for customizing that step. In addition, <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A> is itself a <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> and thus also can be customized. <P>
|
|
The object system specifies system-supplied primary <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A> 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>
|
|
<P><DL><P>
|
|
<DT>* Declaring a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> to be an initialization argument for a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A>. An initialization argument is declared by using the <TT>:initarg</TT> slot option to <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A>. This provides a mechanism for supplying a value for a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> in a call to <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A>. <P><DD>
|
|
<DT>* 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 <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A>. If an initialization argument is not explicitly provided as an argument to <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A>, the default value form is evaluated in the lexical environment of the <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A> form that defined it, and the resulting value is used as the value of the initialization argument. <P><DD>
|
|
<DT>* Supplying a default initial value form for a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A>. A default initial value form for a <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> is defined by using the <TT>:initform</TT> slot option to <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A>. If no initialization argument associated with that <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A> is given as an argument to <A REL=DEFINITION HREF="f_mk_ins.htm#make-instance"><B>make-instance</B></A> or is defaulted by <TT>:default-initargs</TT>, this default initial value form is evaluated in the lexical environment of the <A REL=DEFINITION HREF="m_defcla.htm#defclass"><B>defclass</B></A> form that defined it, and the resulting value is stored in the <A REL=DEFINITION HREF="26_glo_s.htm#slot"><I>slot</I></A>. The <TT>:initform</TT> form for a <A REL=DEFINITION HREF="26_glo_l.htm#local_slot"><I>local slot</I></A> may be used when creating an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A>, when updating an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> to conform to a redefined <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>, or when updating an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> to conform to the definition of a different <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>. The <TT>:initform</TT> form for a <A REL=DEFINITION HREF="26_glo_s.htm#shared_slot"><I>shared slot</I></A> may be used when defining or re-defining the <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>. <P><DD>
|
|
<DT>* Defining <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>methods</I></A> for <A REL=DEFINITION HREF="f_init_i.htm#initialize-instance"><B>initialize-instance</B></A> and <A REL=DEFINITION HREF="f_shared.htm#shared-initialize"><B>shared-initialize</B></A>. The slot-filling behavior described above is implemented by a system-supplied primary <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> for <A REL=DEFINITION HREF="f_init_i.htm#initialize-instance"><B>initialize-instance</B></A> which invokes <A REL=DEFINITION HREF="f_shared.htm#shared-initialize"><B>shared-initialize</B></A>. The <A REL=DEFINITION HREF="26_glo_g.htm#generic_function"><I>generic function</I></A> <A REL=DEFINITION HREF="f_shared.htm#shared-initialize"><B>shared-initialize</B></A> implements the parts of initialization shared by these four situations: when making an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A>, when re-initializing an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A>, when updating an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> to conform to a redefined <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>, and when updating an <A REL=DEFINITION HREF="26_glo_i.htm#instance"><I>instance</I></A> to conform to the definition of a different <A REL=DEFINITION HREF="26_glo_c.htm#class"><I>class</I></A>. The system-supplied primary <A REL=DEFINITION HREF="26_glo_m.htm#method"><I>method</I></A> for <A REL=DEFINITION HREF="f_shared.htm#shared-initialize"><B>shared-initialize</B></A> directly implements the slot-filling behavior described above, and <A REL=DEFINITION HREF="f_init_i.htm#initialize-instance"><B>initialize-instance</B></A> simply invokes <A REL=DEFINITION HREF="f_shared.htm#shared-initialize"><B>shared-initialize</B></A>. <P><DD>
|
|
<P></DL><P>
|
|
|
|
|
|
<A REL=CHILD HREF="07_aa.htm"><H2>
|
|
7.1.1 Initialization Arguments</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_ab.htm"><H2>
|
|
7.1.2 Declaring the Validity of Initialization Arguments</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_ac.htm"><H2>
|
|
7.1.3 Defaulting of Initialization Arguments</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_ad.htm"><H2>
|
|
7.1.4 Rules for Initialization Arguments</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_ae.htm"><H2>
|
|
7.1.5 Shared-Initialize</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_af.htm"><H2>
|
|
7.1.6 Initialize-Instance</H2></A><P>
|
|
|
|
<A REL=CHILD HREF="07_ag.htm"><H2>
|
|
7.1.7 Definitions of Make-Instance and Initialize-Instance</H2></A><P><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|