dict-structures.tex 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. % -*- Mode: TeX -*-
  2. %!!! Need a glossary term which refers to un-:type'd `structures'. -kmp 6-Jun-91
  3. %%% ========== DEFSTRUCT
  4. \begincom{defstruct}\ftype{Macro}
  5. \label Syntax::
  6. \DefmacWithValuesNewline defstruct
  7. {name-and-options \brac{documentation} \stardown{slot-description}}
  8. {structure-name}
  9. \auxbnf{name-and-options}{structure-name |
  10. \paren{structure-name \interleave{\down{options}}}}
  11. \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  12. \auxbnf{options}{\down{conc-name-option} |\CR
  13. \stardown{constructor-option} |\CR
  14. \down{copier-option} |\CR
  15. \down{include-option} |\CR
  16. \down{initial-offset-option} |\CR
  17. \down{named-option} |\CR
  18. \down{predicate-option} |\CR
  19. \down{printer-option} |\CR
  20. \down{type-option}}
  21. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  22. \auxbnf{conc-name-option}{\kwd{conc-name} |
  23. \paren{\kwd{conc-name}} |
  24. \paren{\kwd{conc-name} \param{conc-name}}}
  25. \auxbnf{constructor-option}{\kwd{constructor} |\CR
  26. \paren{\kwd{constructor}} |\CR
  27. \paren{\kwd{constructor} \param{constructor-name}} |\CR
  28. \paren{\kwd{constructor}
  29. \param{constructor-name}
  30. \param{constructor-arglist}}}
  31. \auxbnf{copier-option}{\kwd{copier} |
  32. \paren{\kwd{copier}} |
  33. \paren{\kwd{copier} \param{copier-name}}}
  34. \auxbnf{predicate-option}{\kwd{predicate} |
  35. \paren{\kwd{predicate}} |
  36. \paren{\kwd{predicate} \param{predicate-name}}}
  37. \auxbnf{include-option}{\paren{\kwd{include} \param{included-structure-name}
  38. \stardown{slot-description}}}
  39. \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  40. \auxbnf{printer-option}{\down{print-object-option} | \down{print-function-option}}
  41. \auxbnf{print-object-option}{\paren{\kwd{print-object} \param{printer-name}} |
  42. \paren{\kwd{print-object}}}
  43. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  44. \auxbnf{print-function-option}{\paren{\kwd{print-function} \param{printer-name}} |
  45. \paren{\kwd{print-function}}}
  46. \auxbnf{type-option}{\paren{\kwd{type} \param{type}}}
  47. \auxbnf{named-option}{\kwd{named}}
  48. \auxbnf{initial-offset-option}{\paren{\kwd{initial-offset} \param{initial-offset}}}
  49. %% 19.2.0 6
  50. \auxbnf{slot-description}{\param{slot-name} | \CR
  51. \paren{\param{slot-name}
  52. \brac{\param{slot-initform}
  53. \interleave{\down{slot-option}}}}}
  54. \auxbnf{slot-option}{\kwd{type} \param{slot-type} | \CR
  55. \kwd{read-only} \param{slot-read-only-p}}
  56. \label Arguments and Values::
  57. \param{conc-name}---a \term{\symbolnamedesignator}.
  58. \param{constructor-arglist}---a \term{boa lambda list}.
  59. \param{constructor-name}---a \term{symbol}.
  60. \param{copier-name}---a \term{symbol}.
  61. \param{included-structure-name}---an already-defined \term{structure name}.
  62. \issue{DEFSTRUCT-INCLUDE-DEFTYPE:EXPLICITLY-UNDEFINED}
  63. % or a \term{type specifier} \term{symbol}.
  64. Note that a \term{derived type} is not permissible,
  65. even if it would expand into a \term{structure name}.
  66. % or a \term{type specifier} \term{symbol}.)
  67. \endissue{DEFSTRUCT-INCLUDE-DEFTYPE:EXPLICITLY-UNDEFINED}
  68. \param{initial-offset}---a non-negative \term{integer}.
  69. \param{predicate-name}---a \term{symbol}.
  70. \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  71. \param{printer-name}---a \term{function name} or a \term{lambda expression}.
  72. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  73. %% Per X3J13. -kmp 05-Oct-93
  74. \param{slot-name}---a \term{symbol}.
  75. \param{slot-initform}---a \term{form}.
  76. \param{slot-read-only-p}---a \term{generalized boolean}.
  77. \param{structure-name}---a \term{symbol}.
  78. \param{type}---one of the \term{type specifiers}
  79. \typeref{list},
  80. \typeref{vector},
  81. or \f{(vector \param{size})},
  82. or some other \term{type specifier} defined
  83. by the \term{implementation} to be appropriate.
  84. %% 19.2.0 3
  85. % Removed redundant syntax presentation. -kmp
  86. %% 19.2.0 4
  87. \param{documentation}---a \term{string}; \noeval.
  88. \label Description::
  89. \macref{defstruct} defines a structured \term{type}, named \param{structure-type},
  90. with named slots as specified by the \param{slot-options}.
  91. %% 19.2.0 8
  92. \macref{defstruct} defines \term{readers} for the slots and
  93. arranges for \macref{setf} to work properly on such
  94. \term{reader} functions.
  95. Also, unless overridden, it
  96. defines a predicate named \f{\param{name}-p},
  97. defines a constructor function named \f{make-\param{constructor-name}},
  98. and defines a copier function named \f{copy-\param{constructor-name}}.
  99. %All names of automatically created functions are interned
  100. %in whatever package is current at the time the \macref{defstruct}
  101. %form is expanded.
  102. All names of automatically created functions might automatically
  103. be declared \declref{inline} (at the discretion of the \term{implementation}).
  104. %% 19.2.0 5
  105. If \param{documentation} is supplied, it is attached to \param{structure-name}
  106. as a \term{documentation string} of kind \misc{structure},
  107. \issue{DOCUMENTATION-FUNCTION-BUGS:FIX}
  108. and unless \kwd{type} is used, the \param{documentation} is also attached
  109. to \param{structure-name} as a \term{documentation string} of kind
  110. \misc{type} and as a \term{documentation string} to the \term{class} \term{object}
  111. for the \term{class} named \param{structure-name}.
  112. \endissue{DOCUMENTATION-FUNCTION-BUGS:FIX}
  113. %% 19.3.0 1
  114. \macref{defstruct} defines a constructor function that is used to
  115. create instances of the structure created by \macref{defstruct}.
  116. The default name is \f{make-\param{structure-name}}.
  117. A different name can be supplied
  118. by giving the name as the argument to the \param{constructor} option.
  119. \nil\ indicates that no constructor function will be created.
  120. %% 19.3.0 2
  121. After a new structure type has been defined, instances of that type
  122. normally can be created by using the constructor function for the
  123. type.
  124. A call to a constructor function is of the following form:
  125. \Vskip 1pc!
  126. \settabs\+\hskip\leftskip&(&\cr
  127. \+&(&{\tt constructor-function-name}\cr
  128. \+&&{\tt slot-keyword-1 form-1}\cr
  129. \+&&{\tt slot-keyword-2 form-2}\cr
  130. \+&&$\ldots$)\cr
  131. \Vskip 1pc!
  132. \rm
  133. %% 19.3.0 3
  134. The arguments to the constructor function are all keyword arguments. Each
  135. slot keyword argument must be
  136. a keyword whose name corresponds to the name of a structure slot.
  137. All the \param{keywords} and \param{forms}
  138. are evaluated.
  139. % If a slot is not initialized in this way,
  140. % it is initialized by evaluating \param{slot-initform} in the
  141. % slot description.
  142. % \param{Slot-initform} is evaluated
  143. % \issue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  144. % each time its value is to be used as the initial value of a slot.
  145. %
  146. % %The following will be deleted:
  147. % %
  148. % %If no \param{slot-initform} is
  149. % %supplied, the contents of the slot are \term{implementation-dependent}.
  150. % \endissue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  151. %% Rewritten for Barmar:
  152. If a slot is not initialized in this way,
  153. it is initialized by evaluating \param{slot-initform} in the slot description
  154. \issue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  155. at the time the constructor function is called.
  156. %If no \param{slot-initform} is
  157. %supplied, the contents of the slot are \term{implementation-dependent}.
  158. \endissue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  159. \issue{UNITIALIZED-ELEMENTS:CONSEQUENCES-UNDEFINED}
  160. % Refer to discussion of failed issue UNINITIALIZED-ELEMENTS for an explanation
  161. % of how the next sentence came to be. -kmp 9-Feb-92
  162. % This issue later actually passed at the Mar-93 meeting,
  163. % affirming the wording here but making changes elsewhere (in MAKE-ARRAY and ADJUST-ARRAY).
  164. % -kmp 5-May-93
  165. If no \param{slot-initform} is supplied,
  166. the consequences are undefined if an attempt is later made to read the slot's value
  167. before a value is explicitly assigned.
  168. \endissue{UNITIALIZED-ELEMENTS:CONSEQUENCES-UNDEFINED}
  169. %% 19.3.0 4
  170. Each \param{slot-initform} supplied for a \macref{defstruct} component,
  171. when used by the constructor function for an otherwise unsupplied
  172. component, is re-evaluated on every call to the
  173. constructor function.
  174. \issue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  175. The \param{slot-initform} is not evaluated
  176. unless it is needed in the creation of a particular structure
  177. instance. If it is never needed, there can be no type-mismatch
  178. error, even if the \term{type}
  179. of the slot is specified; no warning
  180. should be issued in this case.
  181. \endissue{DEFSTRUCT-DEFAULT-VALUE-EVALUATION:IFF-NEEDED}
  182. %!!! Barmar: What if no initial value is specified when creating an instance??
  183. For example, in the following sequence, only the last call is an error.
  184. \code
  185. (defstruct person (name 007 :type string))
  186. (make-person :name "James")
  187. (make-person)
  188. \endcode
  189. It is as if the \param{slot-initforms} were
  190. used as \term{initialization forms} for the \term{keyword parameters}
  191. of the constructor function.
  192. \issue{DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:NOT-BOUND}
  193. The \term{symbols} which name the slots must not be used by the
  194. \term{implementation} as the \term{names} for the \term{lambda variables}
  195. in the constructor function, since one or more of those \term{symbols}
  196. might have been proclaimed \declref{special} or might be defined as
  197. the name of a \term{constant variable}.
  198. The slot default init forms are evaluated
  199. in the \term{lexical environment} in which the \macref{defstruct} form itself appears and
  200. in the \term{dynamic environment} in which the call to the constructor function appears.
  201. \endissue{DEFSTRUCT-CONSTRUCTOR-SLOT-VARIABLES:NOT-BOUND}
  202. For example, if the form \f{(gensym)} were used as an initialization form,
  203. either in the constructor-function call or as the default initialization form
  204. in \macref{defstruct}, then every call to the constructor function would call
  205. \funref{gensym} once to generate a new \term{symbol}.
  206. \issue{DEFSTRUCT-SLOTS-CONSTRAINTS-NUMBER}
  207. %% 19.4.0 1
  208. Each \param{slot-description} in \macref{defstruct} can specify zero or more
  209. \param{slot-options}.
  210. \endissue{DEFSTRUCT-SLOTS-CONSTRAINTS-NUMBER}
  211. A \param{slot-option} consists of a pair of a keyword and a value
  212. (which is not a form to be evaluated, but the value itself). For example:
  213. \code
  214. (defstruct ship
  215. (x-position 0.0 :type short-float)
  216. (y-position 0.0 :type short-float)
  217. (x-velocity 0.0 :type short-float)
  218. (y-velocity 0.0 :type short-float)
  219. (mass *default-ship-mass* :type short-float :read-only t))
  220. \endcode
  221. This specifies that each slot always contains a \term{short float},
  222. and that the last slot cannot be altered once a ship is constructed.
  223. %% 19.4.0 2
  224. The available slot-options are:
  225. \beginlist
  226. %% 19.4.0 3
  227. \itemitem{\tt \kwd{type} \param{type}}
  228. This specifies that the contents of the
  229. slot is always of type \param{type}. This is entirely
  230. analogous to the declaration of a variable or function; it
  231. effectively declares the result type of the \term{reader} function.
  232. % Whether an \term{implementation} chooses to check the type of the new \term{object}
  233. % when initializing or assigning to a slot is \term{implementation-dependent}.
  234. %% Rewritten for Barmar:
  235. It is \term{implementation-dependent} whether the \term{type} is checked
  236. when initializing a slot
  237. or when assigning to it.
  238. \param{Type} is not evaluated; it must be a valid \term{type specifier}.
  239. %% 19.4.0 4
  240. \itemitem{\tt \kwd{read-only} \param{x}}
  241. When \param{x} is \term{true},
  242. this specifies that this slot cannot be
  243. altered; it will always contain the value supplied at construction time.
  244. \macref{setf} will not accept the \term{reader} function for this slot.
  245. If \param{x} is \term{false}, this slot-option has no effect.
  246. \param{X} is not evaluated.
  247. \issue{SETF-FUNCTIONS-AGAIN:MINIMAL-CHANGES}
  248. When this option is \term{false} or unsupplied,
  249. it is \term{implementation-dependent} whether the ability to \term{write}
  250. the slot is implemented by a \term{setf function} or a \term{setf expander}.
  251. \endissue{SETF-FUNCTIONS-AGAIN:MINIMAL-CHANGES}
  252. \endlist
  253. %% 19.5.0 3
  254. The following keyword options are available for use with \macref{defstruct}.
  255. A \macref{defstruct} option can be either a keyword or a \term{list}
  256. of a keyword and arguments for that keyword;
  257. % This part added at Barmar's suggestion. Quinquevirate appeared to concur. -kmp 30-Aug-91
  258. specifying the keyword by itself is equivalent to specifying a list consisting of
  259. the keyword and no arguments.
  260. The syntax for \macref{defstruct} options differs from the pair syntax
  261. used for slot-options. No part of any of these options is evaluated.
  262. \beginlist
  263. %% 19.5.0 4
  264. \itemitem{\kwd{conc-name}}
  265. This provides for automatic prefixing of names of \term{reader} (or \term{access}) functions.
  266. The default behavior is to begin the names of all the \term{reader} functions of
  267. a structure with the name of the structure followed by a hyphen.
  268. %% 19.5.0 5
  269. \kwd{conc-name} supplies an alternate
  270. prefix to be used. If a hyphen is to be used as a separator,
  271. it must be supplied as part of the prefix.
  272. If \kwd{conc-name} is \nil\ or no argument is supplied,
  273. then no prefix is used;
  274. then the names of the \term{reader} functions
  275. are the same as the slot names.
  276. If a \term{non-nil} prefix is given,
  277. the name of the \term{reader} \term{function} for each slot is constructed by
  278. concatenating that prefix and the name of the slot, and interning the resulting
  279. \term{symbol} in the \term{package} that is current at the time the
  280. \macref{defstruct} form is expanded.
  281. %% 19.5.0 6
  282. Note that no matter what is supplied for \kwd{conc-name},
  283. slot keywords that match the slot names with no prefix attached are used
  284. with a constructor function.
  285. The \term{reader} function name is used
  286. in conjunction with \macref{setf}. Here is an example:
  287. % \code
  288. % (defstruct door knob-color width material) \EV DOOR
  289. % (setq my-door (make-door :knob-color 'red :width 5.0))
  290. % \EV #S(DOOR :KNOB-COLOR RED :WIDTH 5.0 :MATERIAL NIL)
  291. % (door-width my-door) \EV 5.0
  292. % (setf (door-width my-door) 43.7)
  293. % (door-width my-door) \EV 43.7
  294. % \endcode
  295. %
  296. % Barmar observes that the above didn't demonstrate use of :CONC-NAME...
  297. \code
  298. (defstruct (door (:conc-name dr-)) knob-color width material) \EV DOOR
  299. (setq my-door (make-door :knob-color 'red :width 5.0))
  300. \EV #S(DOOR :KNOB-COLOR RED :WIDTH 5.0 :MATERIAL NIL)
  301. (dr-width my-door) \EV 5.0
  302. (setf (dr-width my-door) 43.7) \EV 43.7
  303. (dr-width my-door) \EV 43.7
  304. \endcode
  305. Whether or not the \kwd{conc-name} option is explicitly supplied,
  306. the following rule governs name conflicts of generated \term{reader}
  307. (or \term{accessor}) names:
  308. For any \term{structure} \term{type} $S\sub 1$
  309. having a \term{reader} function named $R$ for a slot named $X\sub 1$
  310. that is inherited by another \term{structure} \term{type} $S\sub 2$
  311. that would have a \term{reader} function with the same name $R$ for a slot named $X\sub 2$,
  312. no definition for $R$ is generated by the definition of $S\sub 2$;
  313. instead, the definition of $R$ is inherited from the definition of $S\sub 1$.
  314. (In such a case, if $X\sub 1$ and $X\sub 2$ are different slots,
  315. the \term{implementation} might signal a style warning.)
  316. \itemitem{\kwd{constructor}}
  317. %% 19.5.0 7
  318. This option takes zero, one, or two arguments.
  319. If at least one argument is supplied and the first argument is not \nil, then
  320. that argument is a \term{symbol} which specifies the name of the
  321. constructor function. If the argument is not supplied (or if the option itself is not
  322. supplied), the name of the constructor is produced by concatenating the
  323. string \f{"MAKE-"} and the name of the structure, interning the name
  324. in whatever \term{package} is current at the time \macref{defstruct}
  325. is expanded. If the argument is provided and is \nil,
  326. no constructor function is defined.
  327. %% 19.5.0 8
  328. %% 19.6.0 1
  329. If \kwd{constructor} is given as
  330. \f{(:constructor \param{name} \param{arglist})},
  331. then instead of making a keyword
  332. driven constructor function, \macref{defstruct}
  333. defines a ``positional'' constructor function,
  334. taking arguments whose meaning is determined by the argument's position
  335. and possibly by keywords.
  336. \param{Arglist} is used to describe what the arguments to the
  337. constructor will be. In the simplest case something like
  338. \f{(:constructor make-foo (a b c))} defines \f{make-foo} to be
  339. a three-argument
  340. constructor function whose arguments are used to initialize the
  341. slots named \f{a}, \f{b}, and \f{c}.
  342. %% 19.6.0 7
  343. Because a constructor of this type operates ``By Order of Arguments,''
  344. it is sometimes known as a ``boa constructor.''
  345. For information on how the \param{arglist} for a ``boa constructor'' is
  346. processed, \seesection\BoaLambdaLists.
  347. %% 19.6.0 5
  348. It is permissible to use the
  349. \kwd{constructor} option more than once, so that you can define several
  350. different constructor functions, each taking different parameters.
  351. \reviewer{Barmar: What about (:constructor) and (:constructor nil).
  352. Should we worry about it?}%!!!
  353. % I think Barmar was referring to issue DEFSTRUCT-CONSTRUCTOR-OPTIONS,
  354. % which had not been incorporated into the draft. I have added the
  355. % clarification below. --sjl 4 Mar 92
  356. \issue{DEFSTRUCT-CONSTRUCTOR-OPTIONS:EXPLICIT}
  357. \macref{defstruct} creates the default-named keyword constructor function
  358. only if no explicit \kwd{constructor} options are specified, or if the
  359. \kwd{constructor} option is specified without a \param{name} argument.
  360. \f{(:constructor nil)} is meaningful only when there are no other
  361. \kwd{constructor} options specified. It prevents \macref{defstruct}
  362. from generating any constructors at all.
  363. Otherwise, \macref{defstruct} creates a constructor function corresponding
  364. to each supplied \kwd{constructor} option. It is permissible to specify
  365. multiple keyword constructor functions as well as multiple
  366. ``boa constructors''.
  367. \endissue{DEFSTRUCT-CONSTRUCTOR-OPTIONS:EXPLICIT}
  368. \itemitem{\kwd{copier}}
  369. %% 19.5.0 9
  370. This option takes one argument, a \term{symbol},
  371. which specifies the name of the copier
  372. function. If the argument is not provided or if the option itself is not
  373. provided, the name of the copier is produced by concatenating the
  374. string \f{"COPY-"} and the name of the structure, interning the name
  375. in whatever \term{package} is current at the time \macref{defstruct}
  376. is expanded.
  377. If the argument is provided and is \nil, no copier function is defined.
  378. %% 19.5.0 10
  379. \issue{DEFSTRUCT-COPIER:ARGUMENT-TYPE}
  380. % The automatically defined copier function
  381. % makes a new structure
  382. % and transfers all components verbatim from the argument into the
  383. % newly created structure. No attempt is made to make copies
  384. % of the components. Corresponding components of the old and
  385. % new structures are \funref{eql}.
  386. The automatically defined copier function is a function of
  387. one \term{argument},
  388. \issue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT}
  389. which must be of the structure type being defined.
  390. \endissue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT}
  391. The copier function creates a \term{fresh}
  392. structure that has the same \term{type} as its \term{argument},
  393. and that has the \term{same} component values as the original
  394. structure; that is, the component values are not copied recursively.
  395. \issue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT}
  396. If the \macref{defstruct} \kwd{type} option was not used,
  397. % the copier function has the same effect as \funref{copy-structure},
  398. % except that additional type-checking might be performed
  399. % to make sure that the \term{argument} to the copier is
  400. % of \term{type} \param{structure-name}.
  401. the following equivalence applies:
  402. \code
  403. (\param{copier-name} x) = (copy-structure (the \param{structure-name} x))
  404. \endcode
  405. \endissue{DEFSTRUCT-COPIER-ARGUMENT-TYPE:RESTRICT}
  406. \endissue{DEFSTRUCT-COPIER:ARGUMENT-TYPE}
  407. \itemitem{\kwd{include}}
  408. %% 19.5.0 12
  409. This option is used for building a new structure definition as
  410. an extension of another structure definition. For example:
  411. \code
  412. (defstruct person name age sex)
  413. \endcode
  414. To make a new structure to represent an astronaut
  415. that has the
  416. attributes of name, age, and sex, and \term{functions}
  417. that operate on \f{person} structures, \f{astronaut} is defined
  418. with \kwd{include} as follows:
  419. %% 19.5.0 13
  420. \code
  421. (defstruct (astronaut (:include person)
  422. (:conc-name astro-))
  423. helmet-size
  424. (favorite-beverage 'tang))
  425. \endcode
  426. %% 19.5.0 14
  427. \kwd{include} causes the structure being defined
  428. to have the same slots as the included structure.
  429. This is done in such a way
  430. that the \term{reader} functions for the included
  431. structure also work on the structure being defined.
  432. In this example, an
  433. \f{astronaut} therefore has five slots: the three defined in
  434. \f{person} and the two defined in \f{astronaut}
  435. itself. The \term{reader} functions defined by the \f{person} structure
  436. can be applied to instances of the \f{astronaut} structure, and they
  437. work correctly.
  438. Moreover, \f{astronaut} has its own \term{reader} functions for
  439. components defined by the \f{person} structure.
  440. The following examples illustrate the
  441. use of \f{astronaut} structures:
  442. %% 19.5.0 16
  443. \code
  444. (setq x (make-astronaut :name 'buzz
  445. :age 45.
  446. :sex t
  447. :helmet-size 17.5))
  448. (person-name x) \EV BUZZ
  449. (astro-name x) \EV BUZZ
  450. (astro-favorite-beverage x) \EV TANG
  451. \endcode
  452. \issue{REDUCE-ARGUMENT-EXTRACTION}
  453. \code
  454. (reduce #'+ astros :key #'person-age) ; obtains the total of the ages
  455. ; of the possibly empty
  456. ; sequence of astros
  457. \endcode
  458. \endissue{REDUCE-ARGUMENT-EXTRACTION}
  459. The difference between the \term{reader} functions \f{person-name} and \f{astro-name}
  460. is that \f{person-name} can be correctly applied to any \f{person},
  461. including an \f{astronaut}, while \f{astro-name} can be correctly
  462. applied only to an \f{astronaut}. An implementation might
  463. check for incorrect use of \term{reader} functions.
  464. %% 19.5.0 17
  465. At most one \kwd{include} can be supplied in a single \macref{defstruct}.
  466. The argument to \kwd{include} is required and must be the
  467. name of some previously defined structure. If the structure being
  468. defined has no \kwd{type} option, then the included structure must
  469. also have had no \kwd{type} option supplied for it.
  470. If the structure being defined has a \kwd{type} option,
  471. then the included structure must have been declared with a \kwd{type}
  472. option specifying the same representation \term{type}.
  473. %% 19.5.0 18
  474. If no \kwd{type} option is involved, then
  475. the structure name of the including structure definition
  476. becomes the name of a \term{data type}, and therefore
  477. a valid \term{type specifier} recognizable by \funref{typep}; it becomes
  478. a \term{subtype} of the included structure.
  479. In the above example,
  480. \f{astronaut} is a \term{subtype} of \f{person}; hence
  481. \code
  482. (typep (make-astronaut) 'person) \EV \term{true}
  483. \endcode
  484. indicating that all operations on persons also
  485. work on astronauts.
  486. %% 19.5.0 19
  487. The structure using \kwd{include} can specify default values or
  488. slot-options for the included slots different from those the included
  489. structure specifies, by giving the \kwd{include} option as:
  490. \code
  491. (:include \param{included-structure-name} \starparam{slot-description})
  492. \endcode
  493. Each \param{slot-description} must have a \param{slot-name}
  494. %% What was this about? -kmp 9-Feb-92
  495. %or \param{slot-keyword}
  496. that is the same
  497. as that of some slot in the included structure.
  498. If a \param{slot-description} has no \param{slot-initform},
  499. then in the new structure the slot has no initial value.
  500. Otherwise its initial value form is replaced by
  501. the \param{slot-initform} in the \param{slot-description}.
  502. A normally writable slot can be made read-only.
  503. If a slot is read-only in the included structure, then it
  504. must also be so in the including structure.
  505. If a \term{type} is supplied for a slot, it must be
  506. %% Redundant. "subtype" includes "same type". -kmp 30-Aug-91
  507. %the same as, or
  508. a \term{subtype} of
  509. %,
  510. the
  511. \term{type} specified in the included structure.
  512. %%Barmar and I couldn't figure out what this contributed. The implementation
  513. %%can do this anyway, subtype or no. Removed as confusing. -kmp 28-Dec-90
  514. % If it is a strict \term{subtype}, the implementation might choose to
  515. % error-check assignments.
  516. %% 19.5.0 20
  517. For example, if the
  518. default age for an astronaut is \f{45}, then
  519. \code
  520. (defstruct (astronaut (:include person (age 45)))
  521. helmet-size
  522. (favorite-beverage 'tang))
  523. \endcode
  524. %% 19.7.3 1
  525. If \kwd{include} is used with the \kwd{type}
  526. option, then the effect is first to skip over as many representation
  527. elements as needed to represent the included structure, then to
  528. skip over any additional elements supplied by the \kwd{initial-offset}
  529. option, and then to begin allocation of elements from that point.
  530. For example:
  531. \code
  532. (defstruct (binop (:type list) :named (:initial-offset 2))
  533. (operator '? :type symbol)
  534. operand-1
  535. operand-2) \EV BINOP
  536. (defstruct (annotated-binop (:type list)
  537. (:initial-offset 3)
  538. (:include binop))
  539. commutative associative identity) \EV ANNOTATED-BINOP
  540. (make-annotated-binop :operator '*
  541. :operand-1 'x
  542. :operand-2 5
  543. :commutative t
  544. :associative t
  545. :identity 1)
  546. \EV (NIL NIL BINOP * X 5 NIL NIL NIL T T 1)
  547. \endcode
  548. The first two \nil\ elements stem from the \kwd{initial-offset} of \f{2}
  549. in the definition of \f{binop}. The next four elements contain the
  550. structure name and three slots for \f{binop}. The next three \nil\ elements
  551. stem from the \kwd{initial-offset} of \f{3} in the definition of
  552. \f{annotated-binop}. The last three list elements contain the additional
  553. slots for an \f{annotated-binop}.
  554. \itemitem{\kwd{initial-offset}}
  555. %% 19.5.0 31
  556. \kwd{initial-offset} instructs \macref{defstruct} to skip over a certain
  557. number of slots before it starts allocating the slots described in the
  558. body. This option's argument is the number of slots \macref{defstruct}
  559. should skip. \kwd{initial-offset} can be used only if \kwd{type} is also supplied.
  560. \reviewer{Barmar: What are initial values of the skipped slots?}%!!!
  561. %% 19.7.3 1
  562. \kwd{initial-offset} allows
  563. slots to be allocated beginning at a representational
  564. element other than the first. For example, the form
  565. \code
  566. (defstruct (binop (:type list) (:initial-offset 2))
  567. (operator '? :type symbol)
  568. operand-1
  569. operand-2) \EV BINOP
  570. \endcode
  571. would result in the following behavior for \f{make-binop}:
  572. \code
  573. (make-binop :operator '+ :operand-1 'x :operand-2 5)
  574. \EV (NIL NIL + X 5)
  575. (make-binop :operand-2 4 :operator '*)
  576. \EV (NIL NIL * NIL 4)
  577. \endcode
  578. The selector functions
  579. \f{binop-operator}, \f{binop-operand-1},
  580. and {\tt binop-operand-2} would be essentially equivalent to \funref{third},
  581. \funref{fourth}, and \funref{fifth}, respectively.
  582. Similarly, the form
  583. \code
  584. (defstruct (binop (:type list) :named (:initial-offset 2))
  585. (operator '? :type symbol)
  586. operand-1
  587. operand-2) \EV BINOP
  588. \endcode
  589. would result in the following behavior for \f{make-binop}:
  590. \code
  591. (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (NIL NIL BINOP + X 5)
  592. (make-binop :operand-2 4 :operator '*) \EV (NIL NIL BINOP * NIL 4)
  593. \endcode
  594. The first two \nil\ elements stem from the \kwd{initial-offset} of \f{2}
  595. in the definition of \f{binop}. The next four elements contain the
  596. structure name and three slots for \f{binop}.
  597. \itemitem{\kwd{named}}
  598. %% 19.5.0 29
  599. \kwd{named} specifies that the structure is named.
  600. If no \kwd{type} is supplied,
  601. then the structure is always named.
  602. %% 19.7.0 1
  603. %% this paragraph was left out
  604. %% 19.7.1 1
  605. For example:
  606. \code
  607. (defstruct (binop (:type list))
  608. (operator '? :type symbol)
  609. operand-1
  610. operand-2) \EV BINOP
  611. \endcode
  612. This defines a constructor function \f{make-binop} and three
  613. selector functions, namely \f{binop-operator}, \f{binop-operand-1},
  614. and \f{binop-operand-2}. (It does not, however, define a predicate
  615. \f{binop-p}, for reasons explained below.)
  616. %% 19.7.1 2
  617. The effect of \f{make-binop} is simply to construct a list of length three:
  618. \code
  619. (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (+ X 5)
  620. (make-binop :operand-2 4 :operator '*) \EV (* NIL 4)
  621. \endcode
  622. It is just like the function \f{list} except that it takes
  623. keyword arguments and performs slot defaulting appropriate to the \f{binop}
  624. conceptual data type. Similarly, the selector functions
  625. \f{binop-operator}, \f{binop-operand-1},
  626. and \f{binop-operand-2} are essentially equivalent to \funref{car},
  627. \funref{cadr}, and \funref{caddr}, respectively. They might not be
  628. completely equivalent because,
  629. for example, an implementation would be justified in adding error-checking
  630. code to ensure that the argument to each selector function is a length-3
  631. list.
  632. %% 19.7.1 3
  633. \f{binop} is a conceptual data type in that it is not made a part of
  634. the \clisp\ type system. \funref{typep} does not recognize \f{binop} as
  635. a \term{type specifier}, and \funref{type-of} returns \f{list} when
  636. given a \f{binop} structure. There is no way to distinguish a data
  637. structure constructed by \f{make-binop} from any other \term{list} that
  638. happens to have the correct structure.
  639. %% 19.7.1 4
  640. There is not any way to recover the structure name \f{binop} from
  641. a structure created by \f{make-binop}. This can only be done
  642. if the structure is named.
  643. A named structure has the property that, given an instance of the
  644. structure, the structure name (that names the type) can be reliably
  645. recovered. For structures defined
  646. with no \kwd{type} option, the structure name actually becomes part
  647. of the \clisp\ data-type system. \funref{type-of},
  648. when applied to such a structure, returns the structure name
  649. as the \term{type} of the \term{object};
  650. \funref{typep} recognizes
  651. the structure name as a valid \term{type specifier}.
  652. For structures defined with a \kwd{type} option, \funref{type-of}
  653. returns a \term{type specifier} such as \f{list} or \f{(vector t)},
  654. depending on the type supplied to the \kwd{type} option.
  655. The structure name does not become a valid \term{type specifier}.
  656. However,
  657. if the \kwd{named} option is also supplied, then the first component
  658. of the structure (as created by a \macref{defstruct} constructor function)
  659. always contains the structure name. This allows the structure name
  660. to be recovered from an instance of the structure and allows a reasonable
  661. predicate for the conceptual type to be defined:
  662. the automatically defined
  663. \param{name-p} predicate for the structure operates by first
  664. checking that its argument is of the proper type (\typeref{list},
  665. \f{(vector t)},
  666. or whatever) and then checking whether the first component contains
  667. the appropriate type name.
  668. Consider the \f{binop} example shown above, modified only to
  669. include the \kwd{named} option:
  670. \code
  671. (defstruct (binop (:type list) :named)
  672. (operator '? :type symbol)
  673. operand-1
  674. operand-2) \EV BINOP
  675. \endcode
  676. As before, this defines a constructor function \f{make-binop} and three
  677. selector functions \f{binop-operator}, \f{binop-operand-1},
  678. and \f{binop-operand-2}. It also defines a predicate \f{binop-p}.
  679. The effect of \f{make-binop} is now to construct a list of length four:
  680. \code
  681. (make-binop :operator '+ :operand-1 'x :operand-2 5) \EV (BINOP + X 5)
  682. (make-binop :operand-2 4 :operator '*) \EV (BINOP * NIL 4)
  683. \endcode
  684. The structure has the same layout as before except that the structure name
  685. \f{binop} is included as the first list element.
  686. The selector functions
  687. \f{binop-operator}, \f{binop-operand-1},
  688. and \f{binop-operand-2} are essentially equivalent to \funref{cadr},
  689. \funref{caddr}, and \funref{cadddr}, respectively.
  690. The predicate \f{binop-p} is more or less equivalent to this
  691. definition:
  692. \code
  693. (defun binop-p (x)
  694. (and (consp x) (eq (car x) 'binop))) \EV BINOP-P
  695. \endcode
  696. The name \f{binop} is still not a valid \term{type specifier} recognizable
  697. to \funref{typep}, but at least there is a way of distinguishing \f{binop}
  698. structures from other similarly defined structures.
  699. \itemitem{\kwd{predicate}}
  700. %% 19.5.0 11
  701. This option takes one argument, which specifies the name of the type predicate.
  702. If the argument is not supplied or if the option itself is not
  703. supplied, the name of the predicate is made by concatenating the
  704. name of the structure to the string \f{"-P"}, interning the name
  705. in whatever \term{package} is current at the time \macref{defstruct}
  706. is expanded.
  707. If the argument is provided and is \nil, no predicate is defined.
  708. A predicate can be defined only if the structure is named;
  709. if \kwd{type} is supplied and \kwd{named} is not supplied,
  710. then \kwd{predicate} must either be unsupplied or have the value \nil.
  711. \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  712. \itemitem{\kwd{print-function}, \kwd{print-object}}
  713. The \kwd{print-function} and \kwd{print-object}
  714. %% Added per X3J13. -kmp 05-Oct-93
  715. options
  716. specify that a \funref{print-object}
  717. \term{method} for \term{structures} of type \param{structure-name} should be generated.
  718. These options are not synonyms, but do perform a similar service;
  719. the choice of which option (\kwd{print-function} or \kwd{print-object}) is used
  720. affects how the function named \param{printer-name} is called.
  721. Only one of these options may be used, and
  722. %% 19.5.0 21
  723. these options may be used only if \kwd{type} is not supplied.
  724. If the \kwd{print-function} option is used,
  725. then when a structure of type \param{structure-name} is to be printed,
  726. the designated printer function is called on three \term{arguments}:
  727. \beginlist
  728. \itemitem{--} the structure to be printed
  729. (a \term{generalized instance} of \param{structure-name}).
  730. \itemitem{--} a \term{stream} to print to.
  731. \itemitem{--} an \term{integer} indicating the current depth.
  732. The magnitude of this integer may vary between \term{implementations};
  733. however, it can reliably be compared against \varref{*print-level*}
  734. to determine whether depth abbreviation is appropriate.
  735. \endlist
  736. Specifying \f{(:print-function \param{printer-name})}
  737. is approximately equivalent to specifying:
  738. \code
  739. (defmethod print-object ((object \param{structure-name}) stream)
  740. (funcall (function \param{printer-name}) object stream \metaparam{current-print-depth}))
  741. \endcode
  742. where the \metaparam{current-print-depth} represents the printer's belief of
  743. how deep it is currently printing. It is \term{implementation-dependent}
  744. whether \metaparam{current-print-depth} is always 0 and \param{*print-level*},
  745. if \term{non-nil}, is re-bound to successively smaller values as printing
  746. descends recursively, or whether \param{current-print-depth} varies in
  747. value as printing descends recursively and \param{*print-level*} remains
  748. constant during the same traversal.
  749. If the \kwd{print-object} option is used, then
  750. when a structure of type \param{structure-name} is to be printed,
  751. the designated printer function is called on two arguments:
  752. \beginlist
  753. \itemitem{--} the structure to be printed.
  754. \itemitem{--} the stream to print to.
  755. \endlist
  756. Specifying \f{(:print-object \param{printer-name})} is equivalent to specifying:
  757. \code
  758. (defmethod print-object ((object \param{structure-name}) stream)
  759. (funcall (function \param{printer-name}) object stream))
  760. \endcode
  761. %% 19.5.0 22
  762. \issue{DEFSTRUCT-PRINT-FUNCTION-INHERITANCE:YES}
  763. \issue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  764. If no \kwd{type} option is supplied,
  765. and if either a \kwd{print-function} or a \kwd{print-object} option is supplied,
  766. and if no \param{printer-name} is supplied,
  767. then a \funref{print-object} \term{method} \term{specialized} for \param{structure-name}
  768. is generated that calls a function that implements the default printing behavior for
  769. structures using \f{\#S} notation; \seesection\PrintingStructures.
  770. If neither a \kwd{print-function}
  771. nor a \kwd{print-object} option
  772. is supplied,
  773. then \macref{defstruct} does not generate a \funref{print-object} \term{method}
  774. \term{specialized} for \param{structure-name} and some default behavior is inherited
  775. either from a structure named in an \kwd{include} option
  776. or from the default behavior for printing structures;
  777. \seefun{print-object} and \secref\PrintingStructures.
  778. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  779. \endissue{DEFSTRUCT-PRINT-FUNCTION-INHERITANCE:YES}
  780. \issue{PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK}
  781. When \varref{*print-circle*} is \term{true},
  782. a user-defined print function can print \term{objects}
  783. to the supplied \term{stream} using
  784. \funref{write},
  785. \funref{prin1},
  786. \funref{princ},
  787. or \funref{format}
  788. and expect circularities to be detected and printed using the \f{\#\i{n}\#} syntax.
  789. This applies to \term{methods} on \funref{print-object} in addition to
  790. \kwd{print-function} options.
  791. If a user-defined print function prints to a \term{stream} other than the one
  792. that was supplied, then circularity detection starts over for that \term{stream}.
  793. \Seevar{*print-circle*}.
  794. \endissue{PRINT-CIRCLE-STRUCTURE:USER-FUNCTIONS-WORK}
  795. %!!! Barmar:
  796. % Are side effects in :PRINT-FUNCTIONs ok? I guess this implies so.
  797. % We should then require that the implementation only call them once.
  798. % KMP: I'm not so sure. Mail sent to Barmar contesting this. -kmp 30-Aug-91
  799. \endissue{DEFSTRUCT-PRINT-FUNCTION-AGAIN:X3J13-MAR-93}
  800. \itemitem{\kwd{type}}
  801. %% 19.5.0 23
  802. \kwd{type} explicitly specifies the representation to be used for
  803. the structure. Its argument must be one of these \term{types}:
  804. \beginlist
  805. %% 19.5.0 26
  806. \itemitem{\typeref{vector}}
  807. This produces the same result as specifying {\tt (vector t)}.
  808. The structure is represented
  809. as a general \term{vector}, storing components as vector elements.
  810. The first component is vector
  811. element 1 if the structure is \kwd{named}, and element 0 otherwise.
  812. \reviewer{Barmar: Do any implementations create non-simple vectors?}%!!!
  813. %% 19.5.0 27
  814. \itemitem{\f{(vector \param{element-type})}}
  815. The structure is represented as a (possibly specialized) \term{vector}, storing
  816. components as vector elements. Every component must be of a \term{type}
  817. that can be stored in a \term{vector} of the \term{type} specified.
  818. The first component is vector
  819. element 1 if the structure is \kwd{named}, and element 0 otherwise.
  820. The structure can be \kwd{named} only if \thetype{symbol}
  821. is a \term{subtype} of the supplied \param{element-type}.
  822. %% 19.5.0 28
  823. \itemitem{\typeref{list}}
  824. The structure is represented as a \term{list}.
  825. The first component is the \term{cadr} if the structure is \kwd{named},
  826. and the \term{car} if it is not \kwd{named}.
  827. \endlist
  828. %% 19.5.0 24
  829. Specifying this option has the effect of forcing
  830. a specific representation and of forcing the components to be
  831. stored in the order specified in \macref{defstruct}
  832. in corresponding successive elements of the specified representation.
  833. It also prevents the structure name from becoming a valid
  834. \term{type specifier} recognizable by \funref{typep}.
  835. %% The following is an RPG response to a question about a KCL bug.
  836. For example:
  837. \code
  838. (defstruct (quux (:type list) :named) x y)
  839. \endcode
  840. should make a constructor that builds a \term{list} exactly like the one
  841. that \funref{list} produces,
  842. with {\tt quux} as its \term{car}.
  843. If this type is defined:
  844. \code
  845. (deftype quux () '(satisfies quux-p))
  846. \endcode
  847. then this form
  848. \code
  849. (typep (make-quux) 'quux)
  850. \endcode
  851. should return precisely what this one does
  852. \code
  853. (typep (list 'quux nil nil) 'quux)
  854. \endcode
  855. %% end of RPG comment
  856. %% 19.5.0 25
  857. If \kwd{type} is not supplied,
  858. %the structure is represented in an \term{implementation-dependent} manner.
  859. the structure is represented as an \term{object} \oftype{structure-object}.
  860. %%Barmar wanted to just strike this. I couldn't figure out what it was trying to say either,
  861. % so I went along with it. -kmp 28-Dec-90
  862. % \issue{LISP-SYMBOL-REDEFINITION:MAR89-X3J13}
  863. % Except where explicitly allowed, the consequences are undefined if
  864. % a \term{symbol} in \thepackage{common-lisp}
  865. % is defined as the name of a structure type.
  866. % using \macref{defstruct} with no \kwd{type} option.
  867. % \endissue{LISP-SYMBOL-REDEFINITION:MAR89-X3J13}
  868. %% Need more infor about how this interacts with CLOS
  869. \macref{defstruct} without a \kwd{type} option defines a \term{class} with
  870. the structure name as its name. The \term{metaclass} of structure
  871. \term{instances} is \typeref{structure-class}.
  872. \endlist
  873. \issue{DEFSTRUCT-REDEFINITION:ERROR}
  874. The consequences of redefining a \macref{defstruct} structure are undefined.
  875. \endissue{DEFSTRUCT-REDEFINITION:ERROR}
  876. In the case where no \macref{defstruct} options have been supplied,
  877. the following functions are automatically defined to operate
  878. on instances of the new structure:
  879. \beginlist
  880. \itemitem{\bf Predicate}
  881. A predicate with the name \f{\param{structure-name}-p} is defined to
  882. test membership in the structure type. The predicate
  883. \f{(\param{structure-name}-p \param{object})} is \term{true} if an \param{object}
  884. is of this \term{type}; otherwise it is \term{false}. \funref{typep} can also
  885. be used with the name of the new \term{type} to test whether an
  886. \term{object}
  887. belongs to the \term{type}.
  888. Such a function call has the form
  889. \hbox{\f{(typep \param{object} '\param{structure-name})}}.
  890. \itemitem{\bf Component reader functions}
  891. \term{Reader} functions are defined to \term{read} the components of the
  892. structure. For each slot name, there is a corresponding
  893. \term{reader} function with the name \f{\param{structure-name}-\param{slot-name}}.
  894. This function \term{reads} the contents of that slot.
  895. Each \term{reader} function takes one argument, which is
  896. an instance of the structure type.
  897. \macref{setf} can be used with any of these \term{reader} functions
  898. to alter the slot contents.
  899. \itemitem{\bf Constructor function}
  900. A constructor function with the name \f{make-\param{structure-name}}
  901. is defined. This function creates and returns new
  902. instances of the structure type.
  903. \itemitem{\bf Copier function}
  904. A copier function with the name \f{copy-\param{structure-name}} is defined.
  905. The copier function takes an object of the structure type and creates a
  906. new object of the same type that is a copy of the first. The copier
  907. function creates a new structure with the same component entries
  908. as the original. Corresponding components of the two structure instances
  909. are \funref{eql}.
  910. \endlist
  911. %The predicate, \term{reader} function, constructor function, and copier function
  912. %names are all interned in whatever package is current at the
  913. %time \macref{defstruct} is expanded.
  914. \issue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY}
  915. % added qualification about top-level-ness --sjl 5 Mar 92
  916. If a \macref{defstruct} \term{form} appears as a \term{top level form},
  917. the \term{compiler} must make the \term{structure} \term{type} name recognized
  918. as a valid \term{type} name in subsequent declarations (as for \macref{deftype})
  919. and make the structure slot readers known to \macref{setf}. In addition, the
  920. \term{compiler} must save enough information about the \term{structure} \term{type}
  921. so that further \macref{defstruct} definitions can use \kwd{include} in a subsequent
  922. \macref{deftype} in the same \term{file} to refer to the \term{structure} \term{type} name.
  923. The functions which \macref{defstruct} generates are not defined
  924. in the compile time environment, although the \term{compiler} may save enough
  925. information about the functions to code subsequent calls inline.
  926. The \f{\#S} \term{reader macro} might or might not recognize the newly defined
  927. \term{structure} \term{type} name at compile time.
  928. \endissue{COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY}
  929. \label Examples::
  930. %% 19.1.0 5
  931. An example of a structure definition follows:
  932. \code
  933. (defstruct ship
  934. x-position
  935. y-position
  936. x-velocity
  937. y-velocity
  938. mass)
  939. \endcode
  940. This declares that every \f{ship} is an \term{object}
  941. with five named components.
  942. The evaluation of this form does the following:
  943. %% 19.1.0 6
  944. \beginlist
  945. \itemitem{1.}
  946. It defines \f{ship-x-position} to be a function
  947. of one argument, a ship, that returns the \f{x-position}
  948. of the ship; \f{ship-y-position}
  949. and the other components are given similar function definitions.
  950. These functions are called the \term{access} functions, as they
  951. are used to \term{access} elements of the structure.
  952. %% 19.1.0 7
  953. \itemitem{2.}
  954. \f{ship} becomes the name of a \term{type} of which instances
  955. of ships are elements. \f{ship} becomes acceptable to \funref{typep},
  956. for example; \f{(typep x 'ship)} is \term{true} if \f{x} is a ship
  957. and false if \f{x} is any \term{object} other than a ship.
  958. %%Moreover, all ships are instances of the type \f{structure}, because
  959. %%\f{ship} is a subtype of \f{structure}.
  960. %% 19.1.0 8
  961. \itemitem{3.}
  962. A function named \f{ship-p} of
  963. one argument is defined; it is a predicate
  964. that is \term{true} if its argument is a ship and is \term{false} otherwise.
  965. %% 19.1.0 9
  966. \itemitem{4.}
  967. A function called \f{make-ship} is defined that, when invoked,
  968. creates a data structure with five components, suitable for use with
  969. the \term{access} functions. Thus executing
  970. \code
  971. (setq ship2 (make-ship))
  972. \endcode
  973. sets \f{ship2} to a newly created \f{ship} \term{object}.
  974. One can supply the initial values of any desired component in the call
  975. to \f{make-ship} by using keyword arguments in this way:
  976. \code
  977. (setq ship2 (make-ship :mass *default-ship-mass*
  978. :x-position 0
  979. :y-position 0))
  980. \endcode
  981. This constructs a new ship and initializes three of its components.
  982. This function is called the ``constructor function''
  983. because it constructs a new structure.
  984. %% 19.1.0 11
  985. \itemitem{5.}
  986. A function called \f{copy-ship} of one argument
  987. is defined that, when given a \f{ship} \term{object},
  988. creates a new \f{ship} \term{object} that is a copy of the given one.
  989. This function is called the ``copier function.''
  990. \endlist
  991. %% 19.1.0 12
  992. \macref{setf} can be used to alter the components of a \f{ship}:
  993. \code
  994. (setf (ship-x-position ship2) 100)
  995. \endcode
  996. This alters the \f{x-position} of \f{ship2} to be \f{100}.
  997. This works because \macref{defstruct} behaves as if
  998. it generates an appropriate \macref{defsetf}
  999. for each \term{access} function.
  1000. %% 19.1.0 13
  1001. %% this paragraph left out
  1002. \code
  1003. ;;;
  1004. ;;; Example 1
  1005. ;;; define town structure type
  1006. ;;; area, watertowers, firetrucks, population, elevation are its components
  1007. ;;;
  1008. (defstruct town
  1009. area
  1010. watertowers
  1011. (firetrucks 1 :type fixnum) ;an initialized slot
  1012. population
  1013. (elevation 5128 :read-only t)) ;a slot that can't be changed
  1014. \EV TOWN
  1015. ;create a town instance
  1016. (setq town1 (make-town :area 0 :watertowers 0)) \EV #S(TOWN...)
  1017. ;town's predicate recognizes the new instance
  1018. (town-p town1) \EV \term{true}
  1019. ;new town's area is as specified by make-town
  1020. (town-area town1) \EV 0
  1021. ;new town's elevation has initial value
  1022. (town-elevation town1) \EV 5128
  1023. ;setf recognizes reader function
  1024. (setf (town-population town1) 99) \EV 99
  1025. (town-population town1) \EV 99
  1026. ;copier function makes a copy of town1
  1027. (setq town2 (copy-town town1)) \EV #S(TOWN...)
  1028. (= (town-population town1) (town-population town2)) \EV \term{true}
  1029. ;since elevation is a read-only slot, its value can be set only
  1030. ;when the structure is created
  1031. (setq town3 (make-town :area 0 :watertowers 3 :elevation 1200))
  1032. \EV #S(TOWN...)
  1033. ;;;
  1034. ;;; Example 2
  1035. ;;; define clown structure type
  1036. ;;; this structure uses a nonstandard prefix
  1037. ;;;
  1038. (defstruct (clown (:conc-name bozo-))
  1039. (nose-color 'red)
  1040. frizzy-hair-p polkadots) \EV CLOWN
  1041. (setq funny-clown (make-clown)) \EV #S(CLOWN)
  1042. ;use non-default reader name
  1043. (bozo-nose-color funny-clown) \EV RED
  1044. (defstruct (klown (:constructor make-up-klown) ;similar def using other
  1045. (:copier clone-klown) ;customizing keywords
  1046. (:predicate is-a-bozo-p))
  1047. nose-color frizzy-hair-p polkadots) \EV klown
  1048. ;custom constructor now exists
  1049. (fboundp 'make-up-klown) \EV \term{true}
  1050. ;;;
  1051. ;;; Example 3
  1052. ;;; define a vehicle structure type
  1053. ;;; then define a truck structure type that includes
  1054. ;;; the vehicle structure
  1055. ;;;
  1056. (defstruct vehicle name year (diesel t :read-only t)) \EV VEHICLE
  1057. (defstruct (truck (:include vehicle (year 79)))
  1058. load-limit
  1059. (axles 6)) \EV TRUCK
  1060. (setq x (make-truck :name 'mac :diesel t :load-limit 17))
  1061. \EV #S(TRUCK...)
  1062. ;vehicle readers work on trucks
  1063. (vehicle-name x)
  1064. \EV MAC
  1065. ;default taken from :include clause
  1066. (vehicle-year x)
  1067. \EV 79
  1068. (defstruct (pickup (:include truck)) ;pickup type includes truck
  1069. camper long-bed four-wheel-drive) \EV PICKUP
  1070. (setq x (make-pickup :name 'king :long-bed t)) \EV #S(PICKUP...)
  1071. ;:include default inherited
  1072. (pickup-year x) \EV 79
  1073. ;;;
  1074. ;;; Example 4
  1075. ;;; use of BOA constructors
  1076. ;;;
  1077. (defstruct (dfs-boa ;BOA constructors
  1078. (:constructor make-dfs-boa (a b c))
  1079. (:constructor create-dfs-boa
  1080. (a &optional b (c 'cc) &rest d &aux e (f 'ff))))
  1081. a b c d e f) \EV DFS-BOA
  1082. ;a, b, and c set by position, and the rest are uninitialized
  1083. (setq x (make-dfs-boa 1 2 3)) \EV #(DFS-BOA...)
  1084. (dfs-boa-a x) \EV 1
  1085. ;a and b set, c and f defaulted
  1086. (setq x (create-dfs-boa 1 2)) \EV #(DFS-BOA...)
  1087. (dfs-boa-b x) \EV 2
  1088. (eq (dfs-boa-c x) 'cc) \EV \term{true}
  1089. ;a, b, and c set, and the rest are collected into d
  1090. (setq x (create-dfs-boa 1 2 3 4 5 6)) \EV #(DFS-BOA...)
  1091. (dfs-boa-d x) \EV (4 5 6)
  1092. \endcode
  1093. \label Affected By:\None.
  1094. \label Exceptional Situations::
  1095. % Adequately addressed in the packages chapter --sjl 5 Mar 92.
  1096. %\issue{LISP-SYMBOL-REDEFINITION:MAR89-X3J13}
  1097. %The consequences are undefined if a \term{symbol} in \thepackage{common-lisp}
  1098. %is used as the \param{structure-name} argument, or if any of the automatically-created
  1099. %\term{functions} have \term{names} in \thepackage{common-lisp}.
  1100. %\endissue{LISP-SYMBOL-REDEFINITION:MAR89-X3J13}
  1101. \issue{DEFSTRUCT-SLOTS-CONSTRAINTS-NAME:DUPLICATES-ERROR}
  1102. % The consequences are undefined if two slots in a structure type have
  1103. % the same \param{slot-name}; that is, the \funref{symbol-name} of the
  1104. % \param{slot-names} should not be \funref{string=}.
  1105. % This holds when
  1106. % they were both named directly by the same call to \macref{defstruct}
  1107. % or when one is present by virtue of being in an included structure.
  1108. % The situation of expanding a \macref{defstruct} macro with a duplicate
  1109. % slot name should signal an error \oftype{program-error}.
  1110. %% Rewritten for Barmar:
  1111. If any two slot names (whether present directly or inherited by the \kwd{include} option)
  1112. are the \term{same} under \funref{string=},
  1113. \macref{defstruct} should signal an error \oftype{program-error}.
  1114. %(While not yet formally defined, the intent is that
  1115. %the error signaling may occur when compiling a file that contains
  1116. %duplicate names or when evaluating a DEFSTRUCT form with duplicate names
  1117. %in an interpreter.)
  1118. \endissue{DEFSTRUCT-SLOTS-CONSTRAINTS-NAME:DUPLICATES-ERROR}
  1119. \issue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED}
  1120. The consequences are undefined if the \param{included-structure-name}
  1121. does not name a \term{structure type}.
  1122. \endissue{DATA-TYPES-HIERARCHY-UNDERSPECIFIED}
  1123. \label See Also::
  1124. \funref{documentation},
  1125. \funref{print-object},
  1126. \macref{setf},
  1127. \funref{subtypep},
  1128. \funref{type-of},
  1129. \funref{typep},
  1130. {\secref\Compilation}
  1131. \label Notes::
  1132. %\funref{typep} will accept and use the name returned by \macref{defstruct}.
  1133. %% 19.7.2 1
  1134. The \param{printer-name} should observe the values of
  1135. such printer-control variables as \varref{*print-escape*}.
  1136. The restriction against issuing a warning for type mismatches between
  1137. a \param{slot-initform} and the corresponding slot's \kwd{type} option is
  1138. necessary because a \param{slot-initform} must be specified in order to
  1139. specify slot options; in some cases, no suitable default may exist.
  1140. The mechanism by which \macref{defstruct} arranges for slot accessors to
  1141. be usable with \macref{setf} is \term{implementation-dependent};
  1142. for example, it may use \term{setf functions}, \term{setf expanders}, or
  1143. some other \term{implementation-dependent} mechanism known to that
  1144. \term{implementation}'s \term{code} for \macref{setf}.
  1145. \endcom
  1146. %%% ========== DEFSTRUCT
  1147. \begincom{copy-structure}\ftype{Function}
  1148. \issue{DEFSTRUCT-COPIER:ARGUMENT-TYPE}
  1149. \label Syntax::
  1150. \DefunWithValues copy-structure {structure} {copy}
  1151. \label Arguments and Values::
  1152. \param{structure}---a \term{structure}.
  1153. \param{copy}---a copy of the \param{structure}.
  1154. \label Description::
  1155. %% 15.2.0 23
  1156. Returns a \term{copy}\meaning{6} of the \param{structure}.
  1157. Only the \param{structure} itself is copied; not the values of the slots.
  1158. %% Per X3J13. -kmp 05-Oct-93
  1159. \label Examples:\None.
  1160. \label Side Effects:\None.
  1161. \label Affected By:\None.
  1162. \label Exceptional Situations:\None.
  1163. \label See Also::
  1164. the \kwd{copier} option to \funref{defstruct}
  1165. \label Notes::
  1166. The \param{copy} is the \term{same} as the given \param{structure}
  1167. under \funref{equalp}, but not under \funref{equal}.
  1168. \endissue{DEFSTRUCT-COPIER:ARGUMENT-TYPE}
  1169. \endcom