concept-objects.tex 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. % -*- Mode: TeX -*-
  2. %%2.5 objects
  3. The \term{generic function} \funref{make-instance} creates and returns a new
  4. \term{instance} of a \term{class}. The first argument is a \term{class} or
  5. the \term{name} of a \term{class}, and the remaining arguments form an
  6. \newterm{initialization argument list}.
  7. The initialization of a new \term{instance} consists of several distinct
  8. steps, including the following: combining the explicitly supplied initialization
  9. arguments with default values for the unsupplied initialization arguments,
  10. checking the validity of the initialization arguments, allocating storage
  11. for the \term{instance}, filling \term{slots} with
  12. values, and executing user-supplied \term{methods} that perform additional
  13. initialization. Each step of \funref{make-instance} is implemented by a
  14. \term{generic function} to provide a mechanism for customizing that step.
  15. In addition, \funref{make-instance} is itself a \term{generic function}
  16. and thus also can be customized.
  17. The \OS\ specifies system-supplied primary \term{methods} for each step
  18. and thus specifies a well-defined standard behavior for the entire
  19. initialization process. The standard behavior provides four simple
  20. mechanisms for controlling initialization:
  21. \beginlist
  22. \itemitem{\bull} Declaring a \term{symbol} to be an initialization argument
  23. for a \term{slot}. An initialization argument is declared by using the
  24. \kwd{initarg} slot option to \macref{defclass}. This provides a mechanism
  25. for supplying a value for a \term{slot} in a call to \funref{make-instance}.
  26. \itemitem{\bull} Supplying a default value form for an initialization argument.
  27. Default value forms for initialization arguments are defined by using the
  28. \kwd{default-initargs} class option to \macref{defclass}. If an
  29. initialization argument is not explicitly provided
  30. as an argument to \funref{make-instance}, the default value form is
  31. evaluated in the lexical environment of the \macref{defclass} form that
  32. defined it, and the resulting value is used as the value of the
  33. initialization argument.
  34. \itemitem{\bull} Supplying a default initial value form for a \term{slot}.
  35. A default initial value form for a \term{slot} is defined by using the
  36. \kwd{initform} slot option to \macref{defclass}. If no initialization
  37. argument associated with that \term{slot} is given as an argument to
  38. \funref{make-instance} or is defaulted by \kwd{default-initargs}, this
  39. default initial value form is evaluated in the lexical environment of
  40. the \macref{defclass} form that defined it, and the resulting value is
  41. stored in the \term{slot}. The \kwd{initform} form for a
  42. \term{local slot} may be used when creating an \term{instance}, when
  43. updating an \term{instance} to conform to a redefined \term{class},
  44. or when updating an \term{instance} to conform to the definition of a
  45. different \term{class}. The \kwd{initform} form for a
  46. \term{shared slot} may be used when defining or re-defining the \term{class}.
  47. \itemitem{\bull}
  48. Defining \term{methods} for \funref{initialize-instance} and
  49. \funref{shared-initialize}. The slot-filling behavior described above is
  50. implemented by a system-supplied primary \term{method} for
  51. \funref{initialize-instance} which invokes \funref{shared-initialize}. The
  52. \term{generic function} \funref{shared-initialize} implements the parts of
  53. initialization shared by these four situations: when making an \term{instance},
  54. when re-initializing an \term{instance}, when updating an \term{instance}
  55. to conform to a redefined \term{class}, and when updating an \term{instance}
  56. to conform to the definition of a different \term{class}. The system-supplied
  57. primary \term{method} for \funref{shared-initialize} directly implements the
  58. slot-filling behavior described above, and \funref{initialize-instance}
  59. simply invokes \funref{shared-initialize}.
  60. \endlist
  61. \beginsubsection{Initialization Arguments}
  62. An initialization argument controls \term{object} creation and
  63. initialization. It is often convenient to use keyword \term{symbols}
  64. to name initialization arguments, but the \term{name} of an
  65. initialization argument can be any \term{symbol}, including \nil. An
  66. initialization argument can be used in two ways: to fill a \term{slot}
  67. with a value or to provide an argument for an initialization
  68. \term{method}. A single initialization argument can be used for both
  69. purposes.
  70. \issue{PLIST-DUPLICATES:ALLOW}
  71. An \term{initialization argument list} is a
  72. %list of alternating of
  73. \term{property list} of
  74. initialization argument names and values.
  75. Its structure is identical
  76. to a \term{property list} and also
  77. to the portion of an argument list
  78. processed for \keyref{key} parameters.
  79. As in those lists,
  80. if an initialization
  81. argument name appears more than once in an initialization argument list,
  82. the leftmost occurrence supplies the value and the remaining occurrences
  83. are ignored. The arguments to \funref{make-instance} (after the first
  84. argument) form an \term{initialization argument list}.
  85. \issue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  86. % Error-checking
  87. % of initialization argument names is disabled if the keyword argument
  88. % pair whose keyword is \kwd{allow-other-keys} and whose value is
  89. % \term{non-nil} appears in the \term{initialization argument list}.
  90. \endissue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  91. \endissue{PLIST-DUPLICATES:ALLOW}
  92. An initialization argument can be associated with a \term{slot}. If
  93. the initialization argument has a value in the \term{initialization
  94. argument list}, the value is stored into the \term{slot} of the newly
  95. created \term{object}, overriding any \kwd{initform} form associated
  96. with the \term{slot}. A single initialization argument can initialize
  97. more than one \term{slot}. An initialization argument that initializes
  98. a \term{shared slot} stores its value into the \term{shared slot},
  99. replacing any previous value.
  100. An initialization argument can be associated with a \term{method}. When
  101. an \term{object} is created and a particular initialization argument is
  102. supplied, the \term{generic functions} \funref{initialize-instance},
  103. \funref{shared-initialize}, and \funref{allocate-instance} are called
  104. with that initialization argument's name and value as a keyword argument
  105. pair. If a value for the initialization argument is not supplied in the
  106. \term{initialization argument list}, the \term{method}'s
  107. \term{lambda list} supplies a default value.
  108. Initialization arguments are used in four situations: when making an
  109. \term{instance}, when re-initializing an \term{instance}, when updating
  110. an \term{instance} to conform to a redefined \term{class}, and when
  111. updating an \term{instance} to conform to the definition of a different
  112. \term{class}.
  113. Because initialization arguments are used to control the creation and
  114. initialization of an \term{instance} of some particular \term{class},
  115. we say that an initialization argument is
  116. ``an initialization argument for'' that \term{class}.
  117. \endsubsection%{Initialization Arguments}
  118. \beginsubsection{Declaring the Validity of Initialization Arguments}
  119. \DefineSection{DeclaringInitargValidity}
  120. Initialization arguments are checked for validity in each of the four
  121. situations that use them. An initialization argument may be valid in
  122. one situation and not another. For example, the system-supplied
  123. primary \term{method} for \funref{make-instance} defined for
  124. \theclass{standard-class} checks the validity of its initialization arguments
  125. and signals an error if an initialization argument is supplied that is
  126. not declared as valid in that situation.
  127. There are two means for declaring initialization arguments valid.
  128. \beginlist
  129. \itemitem{\bull}
  130. Initialization arguments that fill \term{slots} are declared as valid
  131. by the \kwd{initarg} slot option to \macref{defclass}. The
  132. \kwd{initarg} slot option is inherited from \term{superclasses}. Thus
  133. the set of valid initialization arguments that fill \term{slots} for a
  134. \term{class} is the union of the initialization arguments that fill
  135. \term{slots} declared as valid by that \term{class} and its
  136. \term{superclasses}. Initialization arguments that fill \term{slots}
  137. are valid in all four contexts.
  138. \itemitem{\bull}
  139. Initialization arguments that supply arguments to \term{methods} are
  140. declared as valid by defining those \term{methods}. The keyword name of
  141. each keyword parameter specified in the \term{method}'s
  142. \term{lambda list} becomes an initialization argument for all \term{classes}
  143. for which the \term{method} is applicable.
  144. \issue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  145. The presence of {\allowotherkeys} in the
  146. \term{lambda list} of an applicable method disables validity checking of
  147. initialization arguments.
  148. \endissue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  149. Thus \term{method} inheritance
  150. controls the set of valid initialization arguments that supply arguments
  151. to \term{methods}. The \term{generic functions} for which \term{method}
  152. definitions serve to declare initialization arguments valid are as
  153. follows:
  154. \beginlist
  155. \itemitem{--}
  156. Making an \term{instance} of a \term{class}:
  157. \funref{allocate-instance}, \funref{initialize-instance}, and
  158. \funref{shared-initialize}. Initialization arguments declared as valid
  159. by these \term{methods} are valid when making
  160. an \term{instance} of a \term{class}.
  161. \itemitem{--} Re-initializing an \term{instance}:
  162. \funref{reinitialize-instance} and \funref{shared-initialize}.
  163. Initialization arguments declared as valid by these \term{methods} are
  164. valid when re-initializing an \term{instance}.
  165. \itemitem{--} Updating an \term{instance} to conform to a redefined \term{class}:
  166. \funref{update-instance-for-redefined-class} and \funref{shared-initialize}.
  167. Initialization arguments declared as valid by these \term{methods} are
  168. valid when updating an \term{instance} to conform to a redefined \term{class}.
  169. \itemitem{--} Updating an \term{instance} to conform to the definition of a
  170. different \term{class}:
  171. \funref{update-instance-for-different-class} and \funref{shared-initialize}.
  172. Initialization arguments declared as valid by these \term{methods} are
  173. valid when updating an \term{instance} to conform to the definition
  174. of a different \term{class}.
  175. \endlist
  176. \endlist
  177. The set of valid initialization arguments for a \term{class} is the set of
  178. valid initialization arguments that either fill \term{slots} or supply
  179. arguments to \term{methods}, along with the predefined initialization
  180. argument \kwd{allow-other-keys}. The default value for
  181. \kwd{allow-other-keys} is \nil.
  182. \issue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  183. % The meaning of
  184. % \kwd{allow-other-keys} is the same as when it is passed to an ordinary
  185. % \term{function}.
  186. Validity checking of initialization arguments is disabled if the value of
  187. the initialization argument \kwd{allow-other-keys} is \term{true}.
  188. \endissue{INITIALIZATION-FUNCTION-KEYWORD-CHECKING}
  189. \endsubsection%{Declaring the Validity of Initialization Arguments}
  190. \beginsubsection{Defaulting of Initialization Arguments}
  191. A default value \term{form} can be supplied for an initialization
  192. argument by using the \kwd{default-initargs} \term{class} option. If an
  193. initialization argument is declared valid by some particular \term{class},
  194. its default value form might be specified by a different \term{class}.
  195. In this case \kwd{default-initargs} is used to supply a default value
  196. for an inherited initialization argument.
  197. The \kwd{default-initargs} option is used only to provide default
  198. values for initialization arguments; it does not declare a \term{symbol}
  199. as a valid initialization argument name. Furthermore,
  200. the \kwd{default-initargs} option is used only to provide default values for
  201. initialization arguments when making an \term{instance}.
  202. The argument to the \kwd{default-initargs} class
  203. option is a list of
  204. alternating initialization argument names and \term{forms}.
  205. Each \term{form} is the
  206. default value form for the corresponding initialization
  207. argument. The default value \term{form} of an initialization
  208. argument is used and evaluated only if that initialization argument
  209. does not appear in the arguments to \funref{make-instance} and is not
  210. defaulted by a more specific \term{class}. The default value \term{form} is
  211. evaluated in the lexical environment of the \macref{defclass} form that
  212. supplied it; the resulting value is used as the initialization
  213. argument's value.
  214. The initialization arguments supplied to \funref{make-instance} are combined
  215. with defaulted initialization arguments to produce a
  216. \term{defaulted initialization argument list}. A
  217. \term{defaulted initialization argument list}
  218. is a list of alternating initialization argument names and
  219. values in which unsupplied initialization arguments are defaulted and in
  220. which the explicitly supplied initialization arguments appear earlier in
  221. the list than the defaulted initialization arguments. Defaulted
  222. initialization arguments are ordered according to the order in the
  223. \term{class precedence list} of the \term{classes} that supplied the default values.
  224. There is a distinction between the purposes of the
  225. \kwd{default-initargs} and the \kwd{initform} options with respect to the
  226. initialization of \term{slots}. The \kwd{default-initargs}
  227. class option
  228. provides a mechanism for the user to give a default value \term{form}
  229. for an initialization argument without knowing whether the
  230. initialization argument initializes a \term{slot}
  231. or is passed to a \term{method}.
  232. If that initialization argument is not explicitly supplied in a call
  233. to \funref{make-instance}, the default value \term{form} is used, just
  234. as if it had been supplied in the call. In contrast, the
  235. \kwd{initform} slot option provides a mechanism for the user to give a
  236. default initial value form for a \term{slot}. An \kwd{initform} form is
  237. used to initialize a \term{slot} only if no initialization argument
  238. associated with that \term{slot} is given as an argument to
  239. \funref{make-instance} or is defaulted by \kwd{default-initargs}.
  240. \idxtext{order of evaluation}\idxtext{evaluation order}
  241. The order of evaluation of default value \term{forms} for initialization
  242. arguments and the order of evaluation of \kwd{initform} forms are
  243. undefined. If the order of evaluation is important,
  244. \funref{initialize-instance} or \funref{shared-initialize} \term{methods}
  245. should be used
  246. instead.
  247. \endsubsection%{Defaulting of Initialization Arguments}
  248. \beginsubsection{Rules for Initialization Arguments}
  249. \DefineSection{InitargRules}
  250. The \kwd{initarg} slot option may be specified more than
  251. once for a given \term{slot}.
  252. The following rules specify when initialization arguments may be
  253. multiply defined:
  254. \beginlist
  255. \itemitem{\bull} A given initialization argument can be used to
  256. initialize more than one \term{slot} if the same initialization argument name
  257. appears in more than one \kwd{initarg} slot option.
  258. \itemitem{\bull} A given initialization argument name can appear
  259. in the \term{lambda list} of more than one initialization \term{method}.
  260. \itemitem{\bull} A given initialization argument name can
  261. appear both in an \kwd{initarg} slot option and
  262. in the \term{lambda list}
  263. of an initialization \term{method}.
  264. \endlist
  265. \reviewer{The next three paragraphs could be replaced by ``If two or more
  266. initialization arguments that initialize the same slot appear in the
  267. \term{defaulted initialization argument list}, the leftmost of these supplies
  268. the value, even if they have different names.'' And the rest would follow
  269. from the rules above.}
  270. If two or more initialization arguments that initialize the same
  271. \term{slot} are given in the arguments to \funref{make-instance}, the
  272. leftmost of these initialization arguments in the \term{initialization
  273. argument list} supplies the value, even if the initialization arguments
  274. have different names.
  275. If two or more different initialization arguments that initialize the
  276. same \term{slot} have default values and none is given explicitly in the
  277. arguments to \funref{make-instance}, the initialization argument that
  278. appears in a \kwd{default-initargs} class option in the most specific
  279. of the \term{classes} supplies the value. If a single
  280. \kwd{default-initargs} class option specifies two or more initialization
  281. arguments that initialize the same \term{slot} and none is given
  282. explicitly in the arguments to \funref{make-instance}, the leftmost in
  283. the \kwd{default-initargs} class option supplies the value, and the
  284. values of the remaining default value \term{forms} are ignored.
  285. Initialization arguments given explicitly in the arguments to
  286. \funref{make-instance} appear to the left of defaulted initialization
  287. arguments. Suppose that the classes $C\sub 1$ and $C\sub 2$ supply the
  288. values of defaulted initialization arguments for different \term{slots},
  289. and suppose that $C\sub 1$ is more specific than $C\sub 2$; then the
  290. defaulted initialization argument whose value is supplied by $C\sub 1$
  291. is to the left of the defaulted initialization argument whose value is
  292. supplied by $C\sub 2$ in the \term{defaulted initialization argument
  293. list}. If a single \kwd{default-initargs} class option supplies the
  294. values of initialization arguments for two different \term{slots}, the
  295. initialization argument whose value is specified farther to the left in
  296. the \kwd{default-initargs} class option appears farther to the left in
  297. the \term{defaulted initialization argument list}.
  298. \reviewer{Barmar: End of claim made three paragraphs back.}
  299. If a \term{slot} has both an \kwd{initform} form and an
  300. \kwd{initarg} slot option, and the initialization argument is defaulted
  301. using \kwd{default-initargs} or is supplied to \funref{make-instance},
  302. the captured \kwd{initform} form is neither used nor evaluated.
  303. The following is an example of the above rules:
  304. \code
  305. (defclass q () ((x :initarg a)))
  306. (defclass r (q) ((x :initarg b))
  307. (:default-initargs a 1 b 2))
  308. \endcode
  309. $$\vbox{\halign{\strut#\hfil&\quad\hfil#\hfil&\quad\hfil#\hfil\cr
  310. {}&\bf Defaulted&{}\cr
  311. \bf Form&\bf Initialization Argument List&\bf Contents of Slot X\cr
  312. \noalign{\hrule}
  313. {\tt (make-instance 'r)}&{\tt (a 1 b 2)}&{\tt 1}\cr
  314. {\tt (make-instance 'r 'a 3)}&{\tt (a 3 b 2)}&{\tt 3}\cr
  315. {\tt (make-instance 'r 'b 4)}&{\tt (b 4 a 1)}&{\tt 4}\cr
  316. {\tt (make-instance 'r 'a 1 'a 2)}&{\tt (a 1 a 2 b 2)}&{\tt 1}\cr}}$$
  317. \endsubsection%{Rules for Initialization arguments}
  318. \beginsubsection{Shared-Initialize}
  319. \DefineSection{SharedInitialize}
  320. The \term{generic function} \funref{shared-initialize} is used to fill the
  321. \term{slots}
  322. of an \term{instance}
  323. using initialization arguments and \kwd{initform}
  324. forms when an \term{instance} is created, when an
  325. \term{instance} is re-initialized,
  326. when an \term{instance}
  327. is updated to conform to a redefined \term{class}, and when
  328. an \term{instance} is updated to conform to a different \term{class}.
  329. It uses
  330. standard \term{method} combination. It takes the following arguments: the
  331. \term{instance} to be initialized, a
  332. specification of a set of \term{names} of \term{slots}
  333. \term{accessible} in that \term{instance}, and any number of initialization
  334. arguments. The arguments after the first two must form an
  335. \term{initialization argument list}.
  336. The second argument to \funref{shared-initialize} may be one of the following:
  337. \beginlist
  338. \itemitem{\bull} It can be a (possibly empty) \term{list} of \term{slot} names,
  339. which specifies the set of those \term{slot} names.
  340. % \reviewer{Barmar: This next bullet item is redundant with the previous,
  341. % since NIL -is- a LIST. If there was some confusion, we could say ``(possibly empty)''
  342. % in the previous item.}
  343. %
  344. % \itemitem{\bull} It can be \nil, which specifies the empty set of
  345. % \term{slot} names.
  346. \itemitem{\bull} It can be the symbol \t, which specifies the set of all of the \term{slots}.
  347. \endlist
  348. There is a system-supplied primary \term{method} for \funref{shared-initialize}
  349. whose first \term{parameter specializer} is \theclass{standard-object}.
  350. This \term{method} behaves as follows on each \term{slot},
  351. whether shared or local:
  352. \beginlist
  353. \itemitem{\bull} If an initialization argument in the
  354. \term{initialization argument list} specifies a value for that \term{slot},
  355. that value is stored
  356. into the \term{slot}, even if a value has already been stored in the \term{slot}
  357. before the \term{method} is run.
  358. The affected \term{slots} are independent of which
  359. \term{slots} are indicated by the second argument to \funref{shared-initialize}.
  360. \itemitem{\bull} Any \term{slots}
  361. indicated by the second argument that are still
  362. unbound at this point are initialized according to their
  363. \kwd{initform} forms. For any such \term{slot}
  364. that has an \kwd{initform} form,
  365. that \term{form} is evaluated in the
  366. lexical environment of its defining
  367. \macref{defclass} form and the result is stored into the \term{slot}.
  368. For example,
  369. if a \term{before method} stores a value in the
  370. \term{slot}, the \kwd{initform} form will not be used to supply a value
  371. for the \term{slot}. If
  372. the second argument specifies a \term{name} that does not correspond to any
  373. \term{slots} \term{accessible}
  374. in the \term{instance}, the results are unspecified.
  375. \itemitem{\bull} The rules mentioned in {\secref\InitargRules} are obeyed.
  376. \endlist
  377. The generic function \funref{shared-initialize} is called by the
  378. system-supplied primary \term{methods}
  379. for \funref{reinitialize-instance},
  380. \funref{update-instance-for-different-class},
  381. \funref{update-instance-for-redefined-class}, and
  382. \funref{initialize-instance}. Thus, \term{methods} can be written for
  383. \funref{shared-initialize} to specify actions that should be taken in all of
  384. these contexts.
  385. \endsubsection%{Shared-Initialize}
  386. \beginsubsection{Initialize-Instance}
  387. The \term{generic function} \funref{initialize-instance} is called by
  388. \funref{make-instance} to initialize a newly created \term{instance}.
  389. It uses \term{standard method combination}. \term{Methods} for
  390. \funref{initialize-instance} can be defined in order to perform any
  391. initialization that cannot be achieved
  392. %%This was the only case of a half-glossary-term in the entire spec. -kmp 1-Jan-91
  393. %with the simple \term{slot}-filling mechanisms.
  394. simply by supplying initial values for \term{slots}.
  395. During initialization, \funref{initialize-instance} is invoked
  396. after the following actions have been taken:
  397. \beginlist
  398. \itemitem{\bull} The \term{defaulted initialization argument list}
  399. has been computed by combining the supplied \term{initialization argument list}
  400. with any default initialization arguments for the \term{class}.
  401. \itemitem{\bull} The validity of the \term{defaulted initialization argument list}
  402. has been checked. If any of the initialization arguments has not
  403. been declared as valid, an error is signaled.
  404. \itemitem{\bull} A new \term{instance} whose \term{slots}
  405. are unbound has been created.
  406. \endlist
  407. The generic function \funref{initialize-instance} is called with the
  408. new \term{instance} and the defaulted initialization arguments. There is
  409. a system-supplied primary \term{method} for \funref{initialize-instance}
  410. whose \term{parameter specializer} is \theclass{standard-object}. This
  411. \term{method} calls the generic function
  412. \funref{shared-initialize} to fill in
  413. the \term{slots} according to the initialization arguments and the
  414. \kwd{initform} forms for the \term{slots}; the generic function
  415. \funref{shared-initialize} is called with the following arguments: the \term{instance},
  416. \t, and the defaulted initialization arguments.
  417. Note that \funref{initialize-instance} provides the
  418. \term{defaulted initialization argument list} in its call to \funref{shared-initialize},
  419. so the first step performed by the system-supplied primary \term{method} for
  420. \funref{shared-initialize} takes into account both the initialization
  421. arguments provided in the call to \funref{make-instance} and the
  422. \term{defaulted initialization argument list}.
  423. \term{Methods} for \funref{initialize-instance} can be defined to specify
  424. actions to be taken when an \term{instance} is initialized.
  425. If only \term{after methods} for \funref{initialize-instance} are defined, they will be
  426. run after the system-supplied primary \term{method} for initialization and
  427. therefore will not interfere with the default behavior of
  428. \funref{initialize-instance}.
  429. The \OS\ provides two \term{functions} that are useful in the bodies of
  430. \funref{initialize-instance} methods. \Thefunction{slot-boundp}
  431. returns a \term{generic boolean} value that indicates whether a specified \term{slot} has a
  432. value; this provides a mechanism for writing \term{after methods} for
  433. \funref{initialize-instance} that initialize \term{slots} only if they have
  434. not already been initialized. \Thefunction{slot-makunbound}
  435. causes the \term{slot} to have no value.
  436. \endsubsection%{INITIALIZE-INSTANCE}
  437. \beginsubsection{Definitions of Make-Instance and Initialize-Instance}
  438. The generic function \funref{make-instance} behaves as if it were defined as
  439. follows, except that certain optimizations are permitted:
  440. \code
  441. (defmethod make-instance ((class standard-class) &rest initargs)
  442. ...
  443. (let ((instance (apply #'allocate-instance class initargs)))
  444. (apply #'initialize-instance instance initargs)
  445. instance))
  446. (defmethod make-instance ((class-name symbol) &rest initargs)
  447. (apply #'make-instance (find-class class-name) initargs))
  448. \endcode
  449. %This is the code:
  450. %(defmethod make-instance ((class standard-class) &rest initargs)
  451. % (setq initargs (default-initargs class initargs))
  452. % (let* ((proto (class-prototype class))
  453. % (methods
  454. % (append
  455. % (compute-applicable-methods #'allocate-instance `(,class))
  456. % (compute-applicable-methods #'initialize-instance `(,proto))
  457. % (compute-applicable-methods #'shared-initialize `(,proto nil)))))
  458. % (unless
  459. % (subsetp
  460. % (let ((keys '()))
  461. % (do ((plist initargs (cddr plist)))
  462. % ((null plist) keys)
  463. % (push (car plist) keys)))
  464. % (union
  465. % (class-slot-initargs class)
  466. % (reduce #'union (mapcar #'function-keywords methods))))
  467. % (error ...)))
  468. % (let ((instance (apply #'allocate-instance class initargs)))
  469. % (apply #'initialize-instance instance initargs)
  470. % instance))
  471. The elided code in the definition of \funref{make-instance}
  472. %% Per X3J13. -kmp 05-Oct-93
  473. augments the \f{initargs} with any \term{defaulted initialization arguments} and
  474. checks the
  475. %% Per X3J13. -kmp 05-Oct-93
  476. %supplied
  477. resulting
  478. initialization arguments to determine whether an initialization
  479. argument was supplied that neither filled a \term{slot} nor supplied an argument
  480. to an applicable \term{method}.
  481. %This check could be implemented using the generic functions
  482. % ???\funref{class-prototype},??? \funref{compute-applicable-methods},
  483. %\funref{function-keywords}, and ???\funref{class-slot-initargs}. ???
  484. %See Chapter~3 for a
  485. %description of this initialization argument check.
  486. The generic function \funref{initialize-instance} behaves as if it were
  487. defined as follows, except that certain optimizations are permitted:
  488. \code
  489. (defmethod initialize-instance ((instance standard-object) &rest initargs)
  490. (apply #'shared-initialize instance t initargs)))
  491. \endcode
  492. % Barmar complains that "Programmer Interface level" is not defined.
  493. % Presumably it means "this specification".
  494. % Ditto "the meta-object level" is not defined.
  495. % Presumably it should just be omitted as beyond the scope of this standard,
  496. % or else we should define the term somewhere (e.g., the glossary).
  497. % I decided to just trim it down to where glossary words weren't needed. -kmp 6-Jan-91
  498. These procedures can be customized.
  499. % at either the Programmer Interface level,
  500. % the meta-object level, or both.
  501. Customizing at the Programmer Interface level includes using the
  502. \kwd{initform}, \kwd{initarg}, and \kwd{default-initargs} options to
  503. \macref{defclass}, as well as defining \term{methods}
  504. for \funref{make-instance},
  505. %% Per X3J13. -kmp 05-Oct-93
  506. \funref{allocate-instance},
  507. and \funref{initialize-instance}. It is also possible to define
  508. \term{methods} for \funref{shared-initialize}, which would be invoked by the
  509. generic functions \funref{reinitialize-instance},
  510. \funref{update-instance-for-redefined-class},
  511. \funref{update-instance-for-different-class}, and
  512. \funref{initialize-instance}.
  513. The meta-object level supports additional
  514. customization.
  515. %by allowing methods to be defined on \funref{make-instance},
  516. %???\b{default-initargs}???, and \funref{allocate-instance}.
  517. %Chapters~2 and~3 document each of these generic
  518. %functions and the system-supplied primary methods.
  519. Implementations are permitted to make certain optimizations to
  520. \funref{initialize-instance} and \funref{shared-initialize}.
  521. The description of \funref{shared-initialize} in Chapter~7 mentions the
  522. possible optimizations.
  523. %Because of optimization, the check for valid initialization arguments
  524. %might not be implemented using the generic functions
  525. %???\funref{class-prototype},???
  526. %\funref{compute-applicable-methods}, \funref{function-keywords}, and
  527. %???\funref{class-slot-initargs}???. In addition,
  528. %methods for the generic function
  529. %???\funref{default-initargs},??? and the
  530. %system-supplied primary methods for
  531. %???\funref{allocate-instance}???,
  532. %\funref{initialize-instance}, and \funref{shared-initialize} might not be called on
  533. %every call to \funref{make-instance} or might not receive exactly the
  534. %arguments that would be expected.
  535. \endsubsection%{Definitions of MAKE-INSTANCE and Initialize-Instance}