concept-slots.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. % -*- Mode: TeX -*-
  2. \beginsubSection{Introduction to Slots}
  3. An \term{object} \ofmetaclass{standard-class} has zero or more named
  4. \term{slots}. The \term{slots} of an \term{object} are determined
  5. by the \term{class} of the \term{object}. Each \term{slot} can hold
  6. one value.
  7. \reviewer{Barmar: All symbols are valid variable names. Perhaps this means
  8. to preclude the use of named constants? We have a terminology
  9. problem to solve.}%!!!
  10. The \term{name} of a \term{slot} is a \term{symbol} that is syntactically
  11. valid for use as a variable name.
  12. When a \term{slot} does not have a value, the \term{slot} is said to be
  13. \term{unbound}. When an unbound \term{slot} is read,
  14. \reviewer{Barmar: from an object whose metaclass is standard-class?}
  15. the \term{generic function} \funref{slot-unbound} is invoked. The
  16. system-supplied primary \term{method}
  17. for \funref{slot-unbound}
  18. %Barmar: on STANDARD-CLASS or T?
  19. %KMP: It said T in the signature info for SLOT-UNBOUND so I copied that to here.
  20. on \term{class} \typeref{t} signals an error.
  21. \issue{SLOT-MISSING-VALUES:SPECIFY}
  22. If \funref{slot-unbound} returns, its \term{primary value}
  23. is used that time as the \term{value} of the \term{slot}.
  24. \endissue{SLOT-MISSING-VALUES:SPECIFY}
  25. The default initial value form for a \term{slot} is defined by
  26. the \kwd{initform} slot option. When the \kwd{initform} form is used to
  27. supply a value, it is evaluated in the lexical environment in which
  28. the \macref{defclass} form was evaluated. The \kwd{initform} along with
  29. the lexical environment in which the \macref{defclass} form was evaluated
  30. is called a \term{captured initialization form}.
  31. For more details, \seesection\ObjectCreationAndInit.
  32. A \term{local slot} is defined to be a \term{slot} that is
  33. %Barmar says: ``Poor wording. It's "visible" to anyone calling SLOT-VALUE.''
  34. % Perhaps we mean to be saying "accessible in"? -kmp 11-Oct-90
  35. %%Ok. I'll substitute accessible. -kmp 6-Jan-91
  36. %visible
  37. \term{accessible}
  38. to exactly one \term{instance},
  39. namely the one in which the \term{slot} is allocated.
  40. A \term{shared slot} is defined to be a \term{slot} that is visible to more than one
  41. \term{instance} of a given \term{class} and its \term{subclasses}.
  42. A \term{class} is said to define a \term{slot} with a given \term{name} when
  43. the \macref{defclass} form for that \term{class} contains a \term{slot specifier} with
  44. that \term{name}. Defining a \term{local slot} does not immediately create
  45. a \term{slot}; it causes a \term{slot} to be created each time
  46. an \term{instance} of the \term{class} is created.
  47. Defining a \term{shared slot} immediately creates a \term{slot}.
  48. The \kwd{allocation} slot option to \macref{defclass} controls the kind
  49. of \term{slot} that is defined. If the value of the \kwd{allocation} slot
  50. option is \kwd{instance}, a \term{local slot} is created. If the value of
  51. \kwd{allocation} is \kwd{class}, a \term{shared slot} is created.
  52. A \term{slot} is said to be \term{accessible} in an \term{instance}
  53. of a \term{class} if
  54. the \term{slot} is defined by the \term{class}
  55. of the \term{instance} or is inherited from
  56. a \term{superclass} of that \term{class}.
  57. At most one \term{slot} of a given \term{name} can be
  58. \term{accessible} in an \term{instance}.
  59. A \term{shared slot} defined by a \term{class} is
  60. \term{accessible} in all \term{instances}
  61. of that \term{class}.
  62. A detailed explanation of the inheritance of \term{slots} is given in
  63. \secref\SlotInheritance.
  64. \endsubSection%{Slots}
  65. \beginsubSection{Accessing Slots}
  66. \term{Slots} can be \term{accessed} in two ways: by use of the primitive function
  67. \funref{slot-value} and by use of \term{generic functions} generated by
  68. the \macref{defclass} form.
  69. \Thefunction{slot-value} can be used with any of the \term{slot}
  70. names specified in the \macref{defclass} form to \term{access} a specific
  71. \term{slot} \term{accessible} in an \term{instance} of the given \term{class}.
  72. The macro \macref{defclass} provides syntax for generating \term{methods} to
  73. read and write \term{slots}. If a reader \term{method} is requested,
  74. a \term{method} is automatically generated for reading the value of the
  75. \term{slot}, but no \term{method} for storing a value into it is generated.
  76. If a writer \term{method} is requested, a \term{method} is automatically
  77. generated for storing a value into the \term{slot}, but no \term{method}
  78. for reading its value is generated. If an accessor \term{method} is
  79. requested, a \term{method} for reading the value of the \term{slot} and a
  80. \term{method} for storing a value into the \term{slot} are automatically
  81. generated. Reader and writer \term{methods} are implemented using
  82. \funref{slot-value}.
  83. When a reader or writer \term{method} is specified for a \term{slot}, the
  84. name of the \term{generic function} to which the generated \term{method}
  85. belongs is directly specified. If the \term{name} specified for the writer
  86. \term{method} is the symbol \f{name}, the \term{name} of the
  87. \term{generic function} for writing the \term{slot} is the symbol
  88. \f{name}, and the \term{generic function} takes two arguments: the new
  89. value and the \term{instance}, in that order. If the \term{name} specified
  90. for the accessor \term{method} is the symbol \f{name}, the \term{name} of
  91. the \term{generic function} for reading the \term{slot} is the symbol
  92. \f{name}, and the \term{name} of the \term{generic function} for writing
  93. the \term{slot} is the list \f{(setf name)}.
  94. A \term{generic function} created or modified by supplying \kwd{reader},
  95. \kwd{writer}, or \kwd{accessor} \term{slot} options can be treated exactly
  96. as an ordinary \term{generic function}.
  97. Note that \funref{slot-value} can be used to read or write the value of a
  98. \term{slot} whether or not reader or writer \term{methods} exist for that
  99. \term{slot}. When \funref{slot-value} is used, no reader or writer
  100. \term{methods} are invoked.
  101. The macro \macref{with-slots} can be used to establish a
  102. \term{lexical environment} in which specified \term{slots} are lexically
  103. available as if they were variables. The macro \macref{with-slots}
  104. invokes \thefunction{slot-value} to \term{access} the specified \term{slots}.
  105. The macro \macref{with-accessors} can be used to establish a lexical
  106. environment in which specified \term{slots} are lexically available through
  107. their accessors as if they were variables. The macro \macref{with-accessors}
  108. invokes the appropriate accessors to \term{access} the specified \term{slots}.
  109. %Symbolics thinks this sentence is not meaningful:
  110. %Any accessors specified by \macref{with-accessors} must
  111. %already have been defined before they are used.
  112. \endsubSection%{Accessing Slots}
  113. \beginsubsection{Inheritance of Slots and Slot Options}
  114. \DefineSection{SlotInheritance}
  115. The set of the \term{names} of all \term{slots} \term{accessible}
  116. in an \term{instance} of a \term{class} $C$ is the union of
  117. the sets of \term{names} of \term{slots} defined by $C$ and its
  118. \term{superclasses}. The structure of an \term{instance} is
  119. the set of \term{names} of \term{local slots} in that \term{instance}.
  120. In the simplest case, only one \term{class} among $C$ and its \term{superclasses}
  121. defines a \term{slot} with a given \term{slot} name.
  122. If a \term{slot} is defined by a \term{superclass} of $C$\negthinspace,
  123. the \term{slot} is said to be inherited. The characteristics
  124. of the \term{slot} are determined by the \term{slot specifier}
  125. of the defining \term{class}.
  126. Consider the defining \term{class} for
  127. a slot $S$\negthinspace. If the value of the \kwd{allocation}
  128. slot
  129. option is \kwd{instance}, then $S$ is a \term{local slot} and each
  130. \term{instance}
  131. of $C$ has its own \term{slot} named $S$ that stores its own value. If the
  132. value of the \kwd{allocation} slot
  133. option is \kwd{class}, then $S$
  134. is a \term{shared slot}, the \term{class}
  135. that defined $S$ stores the value, and all
  136. \term{instances} of $C$ can \term{access} that single \term{slot}.
  137. If the \kwd{allocation} slot option is omitted, \kwd{instance} is used.
  138. In general, more than one \term{class} among $C$ and its
  139. \term{superclasses} can
  140. define a \term{slot} with a given \term{name}.
  141. In such cases, only one \term{slot} with
  142. the given name is \term{accessible} in an \term{instance}
  143. of $C$\negthinspace, and
  144. the characteristics of that \term{slot} are
  145. a combination of the several \term{slot}
  146. specifiers, computed as follows:
  147. \beginlist
  148. \itemitem{\bull} All the \term{slot specifiers} for a given \term{slot} name
  149. are ordered from most specific to least specific, according to the order in $C$'s
  150. \term{class precedence list} of the \term{classes} that define them. All references
  151. to the specificity of \term{slot specifiers} immediately below refers to this
  152. ordering.
  153. \itemitem{\bull} The allocation of a \term{slot} is controlled by the most
  154. specific \term{slot specifier}. If the most specific \term{slot specifier}
  155. does not contain an \kwd{allocation} slot option, \kwd{instance} is used.
  156. Less specific \term{slot specifiers} do not affect the allocation.
  157. \itemitem{\bull} The default initial value form for a \term{slot}
  158. is the value of the \kwd{initform} slot option in the most specific
  159. \term{slot specifier} that contains one. If no \term{slot specifier}
  160. contains an \kwd{initform} slot option, the \term{slot}
  161. has no default initial value form.
  162. \itemitem{\bull} The contents of a \term{slot} will always be of type
  163. \f{(and $T\sub 1$ $\ldots$ $T\sub n$)} where $T\sub 1 \ldots T\sub n$ are
  164. the values of the \kwd{type} slot options contained in all of the
  165. \term{slot specifiers}. If no \term{slot specifier} contains the
  166. \kwd{type} slot option, the contents of the \term{slot} will always be
  167. \oftype{t}. The consequences of attempting to store in a \term{slot}
  168. a value that does not satisfy the \term{type} of the \term{slot} are undefined.
  169. \itemitem{\bull} The set of initialization arguments that initialize a
  170. given \term{slot} is the union of the initialization arguments declared in
  171. the \kwd{initarg} slot options in all the \term{slot specifiers}.
  172. \itemitem{\bull} The \term{documentation string} for a \term{slot} is the value of
  173. the \kwd{documentation} slot option in the most specific \term{slot}
  174. specifier that contains one. If no \term{slot specifier} contains a
  175. \kwd{documentation} slot option, the \term{slot} has no \term{documentation string}.
  176. \endlist
  177. A consequence of the allocation rule is that a \term{shared slot} can be
  178. \term{shadowed}. For example, if a class $C\sub 1$ defines
  179. a \term{slot} named $S$
  180. whose value for the \kwd{allocation} slot option is \kwd{class},
  181. that \term{slot} is \term{accessible}
  182. in \term{instances} of $C\sub 1$ and all of its
  183. \term{subclasses}. However, if $C\sub 2$ is a \term{subclass}
  184. of $C\sub 1$ and also
  185. defines a \term{slot} named $S$\negthinspace, $C\sub 1$'s
  186. \term{slot} is not shared
  187. by \term{instances} of $C\sub 2$ and its \term{subclasses}. When a class
  188. $C\sub 1$ defines a \term{shared slot}, any subclass $C\sub 2$ of $C\sub
  189. 1$ will share this single \term{slot}
  190. unless the \macref{defclass} form for
  191. $C\sub 2$ specifies a \term{slot} of the same
  192. \term{name} or there is a \term{superclass}
  193. of $C\sub 2$ that precedes $C\sub 1$ in the \term{class precedence list} of
  194. $C\sub 2$ that defines a \term{slot} of the same name.
  195. A consequence of the type rule is that the value of a \term{slot}
  196. satisfies the type constraint of each \term{slot specifier} that
  197. contributes to that \term{slot}. Because the result of attempting to
  198. store in a \term{slot} a value that does not satisfy the type
  199. constraint for the \term{slot} is undefined, the value in a \term{slot}
  200. might fail to satisfy its type constraint.
  201. The \kwd{reader}, \kwd{writer}, and \kwd{accessor} slot options
  202. create \term{methods} rather than define the characteristics of a \term{slot}.
  203. Reader and writer \term{methods} are inherited in the sense described in
  204. \secref\MethodInheritance.
  205. \term{Methods} that \term{access} \term{slots} use only the name of the
  206. \term{slot} and the \term{type} of the \term{slot}'s value. Suppose
  207. a \term{superclass} provides a \term{method} that expects to \term{access} a
  208. \term{shared slot} of a given \term{name}, and a \term{subclass} defines
  209. a \term{local slot} with the same \term{name}. If the \term{method} provided
  210. by the \term{superclass} is used on an \term{instance} of the \term{subclass},
  211. the \term{method} \term{accesses} the \term{local slot}.
  212. \endsubsection%{Inheritance of Slots and Slot Options}