goops.texi 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2008, 2009, 2011, 2017
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @macro goops
  7. GOOPS
  8. @end macro
  9. @macro guile
  10. Guile
  11. @end macro
  12. @node GOOPS
  13. @chapter GOOPS
  14. @goops{} is the object oriented extension to @guile{}. Its
  15. implementation is derived from @w{STk-3.99.3} by Erick Gallesio and
  16. version 1.3 of Gregor Kiczales' @cite{Tiny-Clos}. It is very close in
  17. spirit to CLOS, the Common Lisp Object System, but is adapted for the
  18. Scheme language.
  19. @goops{} is a full object oriented system, with classes, objects,
  20. multiple inheritance, and generic functions with multi-method
  21. dispatch. Furthermore its implementation relies on a meta object
  22. protocol --- which means that @goops{}'s core operations are themselves
  23. defined as methods on relevant classes, and can be customised by
  24. overriding or redefining those methods.
  25. To start using @goops{} you first need to import the @code{(oop goops)}
  26. module. You can do this at the Guile REPL by evaluating:
  27. @lisp
  28. (use-modules (oop goops))
  29. @end lisp
  30. @findex (oop goops)
  31. @menu
  32. * Copyright Notice::
  33. * Class Definition::
  34. * Instance Creation::
  35. * Slot Options::
  36. * Slot Description Example::
  37. * Methods and Generic Functions::
  38. * Inheritance::
  39. * Introspection::
  40. * GOOPS Error Handling::
  41. * GOOPS Object Miscellany::
  42. * The Metaobject Protocol::
  43. * Redefining a Class::
  44. * Changing the Class of an Instance::
  45. @end menu
  46. @node Copyright Notice
  47. @section Copyright Notice
  48. The material in this chapter is partly derived from the STk Reference
  49. Manual written by Erick Gallesio, whose copyright notice is as follows.
  50. Copyright © 1993-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@@unice.fr>
  51. Permission to use, copy, modify, distribute,and license this
  52. software and its documentation for any purpose is hereby granted,
  53. provided that existing copyright notices are retained in all
  54. copies and that this notice is included verbatim in any
  55. distributions. No written agreement, license, or royalty fee is
  56. required for any of the authorized uses.
  57. This software is provided ``AS IS'' without express or implied
  58. warranty.
  59. The material has been adapted for use in Guile, with the author's
  60. permission.
  61. @node Class Definition
  62. @section Class Definition
  63. A new class is defined with the @code{define-class} syntax:
  64. @findex define-class
  65. @cindex class
  66. @lisp
  67. (define-class @var{class} (@var{superclass} @dots{})
  68. @var{slot-description} @dots{}
  69. @var{class-option} @dots{})
  70. @end lisp
  71. @var{class} is the class being defined. The list of @var{superclass}es
  72. specifies which existing classes, if any, to inherit slots and
  73. properties from. @dfn{Slots} hold per-instance@footnote{Usually --- but
  74. see also the @code{#:allocation} slot option.} data, for instances of
  75. that class --- like ``fields'' or ``member variables'' in other object
  76. oriented systems. Each @var{slot-description} gives the name of a slot
  77. and optionally some ``properties'' of this slot; for example its initial
  78. value, the name of a function which will access its value, and so on.
  79. Class options, slot descriptions and inheritance are discussed more
  80. below.
  81. @cindex slot
  82. @deffn syntax define-class name (super @dots{}) @
  83. slot-definition @dots{} class-option @dots{}
  84. Define a class called @var{name} that inherits from @var{super}s, with
  85. direct slots defined by @var{slot-definition}s and @var{class-option}s.
  86. The newly created class is bound to the variable name @var{name} in the
  87. current environment.
  88. Each @var{slot-definition} is either a symbol that names the slot or a
  89. list,
  90. @example
  91. (@var{slot-name-symbol} . @var{slot-options})
  92. @end example
  93. where @var{slot-name-symbol} is a symbol and @var{slot-options} is a
  94. list with an even number of elements. The even-numbered elements of
  95. @var{slot-options} (counting from zero) are slot option keywords; the
  96. odd-numbered elements are the corresponding values for those keywords.
  97. Each @var{class-option} is an option keyword and corresponding value.
  98. @end deffn
  99. As an example, let us define a type for representing a complex number
  100. in terms of two real numbers.@footnote{Of course Guile already
  101. provides complex numbers, and @code{<complex>} is in fact a predefined
  102. class in GOOPS; but the definition here is still useful as an
  103. example.} This can be done with the following class definition:
  104. @lisp
  105. (define-class <my-complex> (<number>)
  106. r i)
  107. @end lisp
  108. This binds the variable @code{<my-complex>} to a new class whose
  109. instances will contain two slots. These slots are called @code{r} and
  110. @code{i} and will hold the real and imaginary parts of a complex
  111. number. Note that this class inherits from @code{<number>}, which is a
  112. predefined class.@footnote{@code{<number>} is the direct superclass of
  113. the predefined class @code{<complex>}; @code{<complex>} is the
  114. superclass of @code{<real>}, and @code{<real>} is the superclass of
  115. @code{<integer>}.}
  116. Slot options are described in the next section. The possible class
  117. options are as follows.
  118. @deffn {class option} #:metaclass metaclass
  119. The @code{#:metaclass} class option specifies the metaclass of the class
  120. being defined. @var{metaclass} must be a class that inherits from
  121. @code{<class>}. For the use of metaclasses, see @ref{Metaobjects and
  122. the Metaobject Protocol} and @ref{Metaclasses}.
  123. If the @code{#:metaclass} option is absent, GOOPS reuses or constructs a
  124. metaclass for the new class by calling @code{ensure-metaclass}
  125. (@pxref{Class Definition Protocol,, ensure-metaclass}).
  126. @end deffn
  127. @deffn {class option} #:name name
  128. The @code{#:name} class option specifies the new class's name. This
  129. name is used to identify the class whenever related objects - the class
  130. itself, its instances and its subclasses - are printed.
  131. If the @code{#:name} option is absent, GOOPS uses the first argument to
  132. @code{define-class} as the class name.
  133. @end deffn
  134. @node Instance Creation
  135. @section Instance Creation and Slot Access
  136. An instance (or object) of a defined class can be created with
  137. @code{make}. @code{make} takes one mandatory parameter, which is the
  138. class of the instance to create, and a list of optional arguments that
  139. will be used to initialize the slots of the new instance. For instance
  140. the following form
  141. @findex make
  142. @cindex instance
  143. @lisp
  144. (define c (make <my-complex>))
  145. @end lisp
  146. @noindent
  147. creates a new @code{<my-complex>} object and binds it to the Scheme
  148. variable @code{c}.
  149. @deffn generic make
  150. @deffnx method make (class <class>) initarg @dots{}
  151. Create and return a new instance of class @var{class}, initialized using
  152. @var{initarg} @enddots{}.
  153. In theory, @var{initarg} @dots{} can have any structure that is
  154. understood by whatever methods get applied when the @code{initialize}
  155. generic function is applied to the newly allocated instance.
  156. In practice, specialized @code{initialize} methods would normally call
  157. @code{(next-method)}, and so eventually the standard GOOPS
  158. @code{initialize} methods are applied. These methods expect
  159. @var{initargs} to be a list with an even number of elements, where
  160. even-numbered elements (counting from zero) are keywords and
  161. odd-numbered elements are the corresponding values.
  162. GOOPS processes initialization argument keywords automatically for slots
  163. whose definition includes the @code{#:init-keyword} option (@pxref{Slot
  164. Options,, init-keyword}). Other keyword value pairs can only be
  165. processed by an @code{initialize} method that is specialized for the new
  166. instance's class. Any unprocessed keyword value pairs are ignored.
  167. @end deffn
  168. @deffn generic make-instance
  169. @deffnx method make-instance (class <class>) initarg @dots{}
  170. @code{make-instance} is an alias for @code{make}.
  171. @end deffn
  172. The slots of the new complex number can be accessed using
  173. @code{slot-ref} and @code{slot-set!}. @code{slot-set!} sets the value
  174. of an object slot and @code{slot-ref} retrieves it.
  175. @findex slot-set!
  176. @findex slot-ref
  177. @lisp
  178. @group
  179. (slot-set! c 'r 10)
  180. (slot-set! c 'i 3)
  181. (slot-ref c 'r) @result{} 10
  182. (slot-ref c 'i) @result{} 3
  183. @end group
  184. @end lisp
  185. The @code{(oop goops describe)} module provides a @code{describe}
  186. function that is useful for seeing all the slots of an object; it prints
  187. the slots and their values to standard output.
  188. @lisp
  189. (describe c)
  190. @print{}
  191. #<<my-complex> 401d8638> is an instance of class <my-complex>
  192. Slots are:
  193. r = 10
  194. i = 3
  195. @end lisp
  196. @node Slot Options
  197. @section Slot Options
  198. When specifying a slot (in a @code{(define-class @dots{})} form),
  199. various options can be specified in addition to the slot's name. Each
  200. option is specified by a keyword. The list of possible keywords is
  201. as follows.
  202. @deffn {slot option} #:init-value init-value
  203. @deffnx {slot option} #:init-form init-form
  204. @deffnx {slot option} #:init-thunk init-thunk
  205. @deffnx {slot option} #:init-keyword init-keyword
  206. These options provide various ways to specify how to initialize the
  207. slot's value at instance creation time.
  208. @cindex default slot value
  209. @var{init-value} specifies a fixed initial slot value (shared across all
  210. new instances of the class).
  211. @var{init-thunk} specifies a thunk that will provide a default value for
  212. the slot. The thunk is called when a new instance is created and should
  213. return the desired initial slot value.
  214. @var{init-form} specifies a form that, when evaluated, will return
  215. an initial value for the slot. The form is evaluated each time that
  216. an instance of the class is created, in the lexical environment of the
  217. containing @code{define-class} expression.
  218. @var{init-keyword} specifies a keyword that can be used to pass an
  219. initial slot value to @code{make} when creating a new instance.
  220. Note that, since an @code{init-value} value is shared across all
  221. instances of a class, you should only use it when the initial value is
  222. an immutable value, like a constant. If you want to initialize a slot
  223. with a fresh, independently mutable value, you should use
  224. @code{init-thunk} or @code{init-form} instead. Consider the following
  225. example.
  226. @example
  227. (define-class <chbouib> ()
  228. (hashtab #:init-value (make-hash-table)))
  229. @end example
  230. @noindent
  231. Here only one hash table is created and all instances of
  232. @code{<chbouib>} have their @code{hashtab} slot refer to it. In order
  233. to have each instance of @code{<chbouib>} refer to a new hash table, you
  234. should instead write:
  235. @example
  236. (define-class <chbouib> ()
  237. (hashtab #:init-thunk make-hash-table))
  238. @end example
  239. @noindent
  240. or:
  241. @example
  242. (define-class <chbouib> ()
  243. (hashtab #:init-form (make-hash-table)))
  244. @end example
  245. If more than one of these options is specified for the same slot, the
  246. order of precedence, highest first is
  247. @itemize @bullet
  248. @item
  249. @code{#:init-keyword}, if @var{init-keyword} is present in the options
  250. passed to @code{make}
  251. @item
  252. @code{#:init-thunk}, @code{#:init-form} or @code{#:init-value}.
  253. @end itemize
  254. If the slot definition contains more than one initialization option of
  255. the same precedence, the later ones are ignored. If a slot is not
  256. initialized at all, its value is unbound.
  257. In general, slots that are shared between more than one instance are
  258. only initialized at new instance creation time if the slot value is
  259. unbound at that time. However, if the new instance creation specifies
  260. a valid init keyword and value for a shared slot, the slot is
  261. re-initialized regardless of its previous value.
  262. Note, however, that the power of GOOPS' metaobject protocol means that
  263. everything written here may be customized or overridden for particular
  264. classes! The slot initializations described here are performed by the least
  265. specialized method of the generic function @code{initialize}, whose
  266. signature is
  267. @example
  268. (define-method (initialize (object <object>) initargs) ...)
  269. @end example
  270. The initialization of instances of any given class can be customized by
  271. defining a @code{initialize} method that is specialized for that class,
  272. and the author of the specialized method may decide to call
  273. @code{next-method} - which will result in a call to the next less
  274. specialized @code{initialize} method - at any point within the
  275. specialized code, or maybe not at all. In general, therefore, the
  276. initialization mechanisms described here may be modified or overridden by
  277. more specialized code, or may not be supported at all for particular
  278. classes.
  279. @end deffn
  280. @deffn {slot option} #:getter getter
  281. @deffnx {slot option} #:setter setter
  282. @deffnx {slot option} #:accessor accessor
  283. Given an object @var{obj} with slots named @code{foo} and @code{bar}, it
  284. is always possible to read and write those slots by calling
  285. @code{slot-ref} and @code{slot-set!} with the relevant slot name; for
  286. example:
  287. @example
  288. (slot-ref @var{obj} 'foo)
  289. (slot-set! @var{obj} 'bar 25)
  290. @end example
  291. The @code{#:getter}, @code{#:setter} and @code{#:accessor} options, if
  292. present, tell GOOPS to create generic function and method definitions
  293. that can be used to get and set the slot value more conveniently.
  294. @var{getter} specifies a generic function to which GOOPS will add a
  295. method for getting the slot value. @var{setter} specifies a generic
  296. function to which GOOPS will add a method for setting the slot value.
  297. @var{accessor} specifies an accessor to which GOOPS will add methods for
  298. both getting and setting the slot value.
  299. So if a class includes a slot definition like this:
  300. @example
  301. (c #:getter get-count #:setter set-count #:accessor count)
  302. @end example
  303. GOOPS defines generic function methods such that the slot value can be
  304. referenced using either the getter or the accessor -
  305. @example
  306. (let ((current-count (get-count obj))) @dots{})
  307. (let ((current-count (count obj))) @dots{})
  308. @end example
  309. - and set using either the setter or the accessor -
  310. @example
  311. (set-count obj (+ 1 current-count))
  312. (set! (count obj) (+ 1 current-count))
  313. @end example
  314. Note that
  315. @itemize @bullet
  316. @item
  317. with an accessor, the slot value is set using the generalized
  318. @code{set!} syntax
  319. @item
  320. in practice, it is unusual for a slot to use all three of these options:
  321. read-only, write-only and read-write slots would typically use only
  322. @code{#:getter}, @code{#:setter} and @code{#:accessor} options
  323. respectively.
  324. @end itemize
  325. The binding of the specified names is done in the environment of the
  326. @code{define-class} expression. If the names are already bound (in that
  327. environment) to values that cannot be upgraded to generic functions,
  328. those values are overwritten when the @code{define-class} expression is
  329. evaluated. For more detail, see @ref{Generic Function Internals,,
  330. ensure-generic}.
  331. @end deffn
  332. @deffn {slot option} #:allocation allocation
  333. The @code{#:allocation} option tells GOOPS how to allocate storage for
  334. the slot. Possible values for @var{allocation} are
  335. @itemize @bullet
  336. @item @code{#:instance}
  337. @findex #:instance
  338. Indicates that GOOPS should create separate storage for this slot in
  339. each new instance of the containing class (and its subclasses). This is
  340. the default.
  341. @item @code{#:class}
  342. @findex #:class
  343. Indicates that GOOPS should create storage for this slot that is shared
  344. by all instances of the containing class (and its subclasses). In other
  345. words, a slot in class @var{C} with allocation @code{#:class} is shared
  346. by all @var{instance}s for which @code{(is-a? @var{instance} @var{c})}.
  347. This permits defining a kind of global variable which can be accessed
  348. only by (in)direct instances of the class which defines the slot.
  349. @item @code{#:each-subclass}
  350. @findex #:each-subclass
  351. Indicates that GOOPS should create storage for this slot that is shared
  352. by all @emph{direct} instances of the containing class, and that
  353. whenever a subclass of the containing class is defined, GOOPS should
  354. create a new storage for the slot that is shared by all @emph{direct}
  355. instances of the subclass. In other words, a slot with allocation
  356. @code{#:each-subclass} is shared by all instances with the same
  357. @code{class-of}.
  358. @item @code{#:virtual}
  359. @findex #:slot-set!
  360. @findex #:slot-ref
  361. @findex #:virtual
  362. Indicates that GOOPS should not allocate storage for this slot. The
  363. slot definition must also include the @code{#:slot-ref} and
  364. @code{#:slot-set!} options to specify how to reference and set the value
  365. for this slot. See the example below.
  366. @end itemize
  367. Slot allocation options are processed when defining a new class by the
  368. generic function @code{compute-get-n-set}, which is specialized by the
  369. class's metaclass. Hence new types of slot allocation can be
  370. implemented by defining a new metaclass and a method for
  371. @code{compute-get-n-set} that is specialized for the new metaclass. For
  372. an example of how to do this, see @ref{Customizing Class Definition}.
  373. @end deffn
  374. @deffn {slot option} #:slot-ref getter
  375. @deffnx {slot option} #:slot-set! setter
  376. The @code{#:slot-ref} and @code{#:slot-set!} options must be specified
  377. if the slot allocation is @code{#:virtual}, and are ignored otherwise.
  378. @var{getter} should be a closure taking a single @var{instance} parameter
  379. that returns the current slot value. @var{setter} should be a closure
  380. taking two parameters - @var{instance} and @var{new-val} - that sets the
  381. slot value to @var{new-val}.
  382. @end deffn
  383. @node Slot Description Example
  384. @section Illustrating Slot Description
  385. To illustrate slot description, we can redefine the @code{<my-complex>}
  386. class seen before. A definition could be:
  387. @lisp
  388. (define-class <my-complex> (<number>)
  389. (r #:init-value 0 #:getter get-r #:setter set-r! #:init-keyword #:r)
  390. (i #:init-value 0 #:getter get-i #:setter set-i! #:init-keyword #:i))
  391. @end lisp
  392. @noindent
  393. With this definition, the @code{r} and @code{i} slots are set to 0 by
  394. default, and can be initialised to other values by calling @code{make}
  395. with the @code{#:r} and @code{#:i} keywords. Also the generic functions
  396. @code{get-r}, @code{set-r!}, @code{get-i} and @code{set-i!} are
  397. automatically defined to read and write the slots.
  398. @lisp
  399. (define c1 (make <my-complex> #:r 1 #:i 2))
  400. (get-r c1) @result{} 1
  401. (set-r! c1 12)
  402. (get-r c1) @result{} 12
  403. (define c2 (make <my-complex> #:r 2))
  404. (get-r c2) @result{} 2
  405. (get-i c2) @result{} 0
  406. @end lisp
  407. Accessors can both read and write a slot. So, another definition of the
  408. @code{<my-complex>} class, using the @code{#:accessor} option, could be:
  409. @findex set!
  410. @lisp
  411. (define-class <my-complex> (<number>)
  412. (r #:init-value 0 #:accessor real-part #:init-keyword #:r)
  413. (i #:init-value 0 #:accessor imag-part #:init-keyword #:i))
  414. @end lisp
  415. @noindent
  416. With this definition, the @code{r} slot can be read with:
  417. @lisp
  418. (real-part c)
  419. @end lisp
  420. @noindent
  421. and set with:
  422. @lisp
  423. (set! (real-part c) new-value)
  424. @end lisp
  425. Suppose now that we want to manipulate complex numbers with both
  426. rectangular and polar coordinates. One solution could be to have a
  427. definition of complex numbers which uses one particular representation
  428. and some conversion functions to pass from one representation to the
  429. other. A better solution is to use virtual slots, like this:
  430. @lisp
  431. (define-class <my-complex> (<number>)
  432. ;; True slots use rectangular coordinates
  433. (r #:init-value 0 #:accessor real-part #:init-keyword #:r)
  434. (i #:init-value 0 #:accessor imag-part #:init-keyword #:i)
  435. ;; Virtual slots access do the conversion
  436. (m #:accessor magnitude #:init-keyword #:magn
  437. #:allocation #:virtual
  438. #:slot-ref (lambda (o)
  439. (let ((r (slot-ref o 'r)) (i (slot-ref o 'i)))
  440. (sqrt (+ (* r r) (* i i)))))
  441. #:slot-set! (lambda (o m)
  442. (let ((a (slot-ref o 'a)))
  443. (slot-set! o 'r (* m (cos a)))
  444. (slot-set! o 'i (* m (sin a))))))
  445. (a #:accessor angle #:init-keyword #:angle
  446. #:allocation #:virtual
  447. #:slot-ref (lambda (o)
  448. (atan (slot-ref o 'i) (slot-ref o 'r)))
  449. #:slot-set! (lambda(o a)
  450. (let ((m (slot-ref o 'm)))
  451. (slot-set! o 'r (* m (cos a)))
  452. (slot-set! o 'i (* m (sin a)))))))
  453. @end lisp
  454. In this class definition, the magnitude @code{m} and angle @code{a}
  455. slots are virtual, and are calculated, when referenced, from the normal
  456. (i.e.@: @code{#:allocation #:instance}) slots @code{r} and @code{i}, by
  457. calling the function defined in the relevant @code{#:slot-ref} option.
  458. Correspondingly, writing @code{m} or @code{a} leads to calling the
  459. function defined in the @code{#:slot-set!} option. Thus the
  460. following expression
  461. @findex #:slot-set!
  462. @findex #:slot-ref
  463. @lisp
  464. (slot-set! c 'a 3)
  465. @end lisp
  466. @noindent
  467. permits to set the angle of the @code{c} complex number.
  468. @lisp
  469. (define c (make <my-complex> #:r 12 #:i 20))
  470. (real-part c) @result{} 12
  471. (angle c) @result{} 1.03037682652431
  472. (slot-set! c 'i 10)
  473. (set! (real-part c) 1)
  474. (describe c)
  475. @print{}
  476. #<<my-complex> 401e9b58> is an instance of class <my-complex>
  477. Slots are:
  478. r = 1
  479. i = 10
  480. m = 10.0498756211209
  481. a = 1.47112767430373
  482. @end lisp
  483. Since initialization keywords have been defined for the four slots, we
  484. can now define the standard Scheme primitives @code{make-rectangular}
  485. and @code{make-polar}.
  486. @lisp
  487. (define make-rectangular
  488. (lambda (x y) (make <my-complex> #:r x #:i y)))
  489. (define make-polar
  490. (lambda (x y) (make <my-complex> #:magn x #:angle y)))
  491. @end lisp
  492. @node Methods and Generic Functions
  493. @section Methods and Generic Functions
  494. A GOOPS method is like a Scheme procedure except that it is specialized
  495. for a particular set of argument classes, and will only be used when the
  496. actual arguments in a call match the classes in the method definition.
  497. @lisp
  498. (define-method (+ (x <string>) (y <string>))
  499. (string-append x y))
  500. (+ "abc" "de") @result{} "abcde"
  501. @end lisp
  502. A method is not formally associated with any single class (as it is in
  503. many other object oriented languages), because a method can be
  504. specialized for a combination of several classes. If you've studied
  505. object orientation in non-Lispy languages, you may remember discussions
  506. such as whether a method to stretch a graphical image around a surface
  507. should be a method of the image class, with a surface as a parameter, or
  508. a method of the surface class, with an image as a parameter. In GOOPS
  509. you'd just write
  510. @lisp
  511. (define-method (stretch (im <image>) (sf <surface>))
  512. ...)
  513. @end lisp
  514. @noindent
  515. and the question of which class the method is more associated with does
  516. not need answering.
  517. There can simultaneously be several methods with the same name but
  518. different sets of specializing argument classes; for example:
  519. @lisp
  520. (define-method (+ (x <string>) (y <string)) ...)
  521. (define-method (+ (x <matrix>) (y <matrix>)) ...)
  522. (define-method (+ (f <fish>) (b <bicycle>)) ...)
  523. (define-method (+ (a <foo>) (b <bar>) (c <baz>)) ...)
  524. @end lisp
  525. @noindent
  526. A generic function is a container for the set of such methods that a
  527. program intends to use.
  528. If you look at a program's source code, and see @code{(+ x y)} somewhere
  529. in it, conceptually what is happening is that the program at that point
  530. calls a generic function (in this case, the generic function bound to
  531. the identifier @code{+}). When that happens, Guile works out which of
  532. the generic function's methods is the most appropriate for the arguments
  533. that the function is being called with; then it evaluates the method's
  534. code with the arguments as formal parameters. This happens every time
  535. that a generic function call is evaluated --- it isn't assumed that a
  536. given source code call will end up invoking the same method every time.
  537. Defining an identifier as a generic function is done with the
  538. @code{define-generic} macro. Definition of a new method is done with
  539. the @code{define-method} macro. Note that @code{define-method}
  540. automatically does a @code{define-generic} if the identifier concerned
  541. is not already a generic function, so often an explicit
  542. @code{define-generic} call is not needed.
  543. @findex define-generic
  544. @findex define-method
  545. @deffn syntax define-generic symbol
  546. Create a generic function with name @var{symbol} and bind it to the
  547. variable @var{symbol}. If @var{symbol} was previously bound to a Scheme
  548. procedure (or procedure-with-setter), the old procedure (and setter) is
  549. incorporated into the new generic function as its default procedure (and
  550. setter). Any other previous value, including an existing generic
  551. function, is discarded and replaced by a new, empty generic function.
  552. @end deffn
  553. @deffn syntax define-method (generic parameter @dots{}) body @dots{}
  554. Define a method for the generic function or accessor @var{generic} with
  555. parameters @var{parameter}s and body @var{body} @enddots{}.
  556. @var{generic} is a generic function. If @var{generic} is a variable
  557. which is not yet bound to a generic function object, the expansion of
  558. @code{define-method} will include a call to @code{define-generic}. If
  559. @var{generic} is @code{(setter @var{generic-with-setter})}, where
  560. @var{generic-with-setter} is a variable which is not yet bound to a
  561. generic-with-setter object, the expansion will include a call to
  562. @code{define-accessor}.
  563. Each @var{parameter} must be either a symbol or a two-element list
  564. @code{(@var{symbol} @var{class})}. The symbols refer to variables in
  565. the body forms that will be bound to the parameters supplied by the
  566. caller when calling this method. The @var{class}es, if present,
  567. specify the possible combinations of parameters to which this method
  568. can be applied.
  569. @var{body} @dots{} are the bodies of the method definition.
  570. @end deffn
  571. @code{define-method} expressions look a little like Scheme procedure
  572. definitions of the form
  573. @example
  574. (define (name formals @dots{}) . body)
  575. @end example
  576. The important difference is that each formal parameter, apart from the
  577. possible ``rest'' argument, can be qualified by a class name:
  578. @code{@var{formal}} becomes @code{(@var{formal} @var{class})}. The
  579. meaning of this qualification is that the method being defined
  580. will only be applicable in a particular generic function invocation if
  581. the corresponding argument is an instance of @code{@var{class}} (or one of
  582. its subclasses). If more than one of the formal parameters is qualified
  583. in this way, then the method will only be applicable if each of the
  584. corresponding arguments is an instance of its respective qualifying class.
  585. Note that unqualified formal parameters act as though they are qualified
  586. by the class @code{<top>}, which GOOPS uses to mean the superclass of
  587. all valid Scheme types, including both primitive types and GOOPS classes.
  588. For example, if a generic function method is defined with
  589. @var{parameter}s @code{(s1 <square>)} and @code{(n <number>)}, that
  590. method is only applicable to invocations of its generic function that
  591. have two parameters where the first parameter is an instance of the
  592. @code{<square>} class and the second parameter is a number.
  593. @menu
  594. * Accessors::
  595. * Extending Primitives::
  596. * Merging Generics::
  597. * Next-method::
  598. * Generic Function and Method Examples::
  599. * Handling Invocation Errors::
  600. @end menu
  601. @node Accessors
  602. @subsection Accessors
  603. An accessor is a generic function that can also be used with the
  604. generalized @code{set!} syntax (@pxref{Procedures with Setters}). Guile
  605. will handle a call like
  606. @example
  607. (set! (@code{accessor} @code{args}@dots{}) @code{value})
  608. @end example
  609. @noindent
  610. by calling the most specialized method of @code{accessor} that matches
  611. the classes of @code{args} and @code{value}. @code{define-accessor} is
  612. used to bind an identifier to an accessor.
  613. @deffn syntax define-accessor symbol
  614. Create an accessor with name @var{symbol} and bind it to the variable
  615. @var{symbol}. If @var{symbol} was previously bound to a Scheme
  616. procedure (or procedure-with-setter), the old procedure (and setter) is
  617. incorporated into the new accessor as its default procedure (and
  618. setter). Any other previous value, including an existing generic
  619. function or accessor, is discarded and replaced by a new, empty
  620. accessor.
  621. @end deffn
  622. @node Extending Primitives
  623. @subsection Extending Primitives
  624. Many of Guile's primitive procedures can be extended by giving them a
  625. generic function definition that operates in conjunction with their
  626. normal C-coded implementation. When a primitive is extended in this
  627. way, it behaves like a generic function with the C-coded implementation
  628. as its default method.
  629. This extension happens automatically if a method is defined (by a
  630. @code{define-method} call) for a variable whose current value is a
  631. primitive. But it can also be forced by calling
  632. @code{enable-primitive-generic!}.
  633. @deffn {primitive procedure} enable-primitive-generic! primitive
  634. Force the creation of a generic function definition for
  635. @var{primitive}.
  636. @end deffn
  637. Once the generic function definition for a primitive has been created,
  638. it can be retrieved using @code{primitive-generic-generic}.
  639. @deffn {primitive procedure} primitive-generic-generic primitive
  640. Return the generic function definition of @var{primitive}.
  641. @code{primitive-generic-generic} raises an error if @var{primitive}
  642. is not a primitive with generic capability.
  643. @end deffn
  644. @node Merging Generics
  645. @subsection Merging Generics
  646. GOOPS generic functions and accessors often have short, generic names.
  647. For example, if a vector package provides an accessor for the X
  648. coordinate of a vector, that accessor may just be called @code{x}. It
  649. doesn't need to be called, for example, @code{vector:x}, because
  650. GOOPS will work out, when it sees code like @code{(x @var{obj})}, that
  651. the vector-specific method of @code{x} should be called if @var{obj} is
  652. a vector.
  653. That raises the question, though, of what happens when different
  654. packages define a generic function with the same name. Suppose we work
  655. with a graphical package which needs to use two independent vector
  656. packages for 2D and 3D vectors respectively. If both packages export
  657. @code{x}, what does the code using those packages end up with?
  658. @ref{Creating Guile Modules,,duplicate binding handlers} explains how
  659. this is resolved for conflicting bindings in general. For generics,
  660. there is a special duplicates handler, @code{merge-generics}, which
  661. tells the module system to merge generic functions with the same name.
  662. Here is an example:
  663. @lisp
  664. (define-module (math 2D-vectors)
  665. #:use-module (oop goops)
  666. #:export (x y ...))
  667. (define-module (math 3D-vectors)
  668. #:use-module (oop goops)
  669. #:export (x y z ...))
  670. (define-module (my-module)
  671. #:use-module (oop goops)
  672. #:use-module (math 2D-vectors)
  673. #:use-module (math 3D-vectors)
  674. #:duplicates (merge-generics))
  675. @end lisp
  676. The generic function @code{x} in @code{(my-module)} will now incorporate
  677. all of the methods of @code{x} from both imported modules.
  678. To be precise, there will now be three distinct generic functions named
  679. @code{x}: @code{x} in @code{(math 2D-vectors)}, @code{x} in @code{(math
  680. 3D-vectors)}, and @code{x} in @code{(my-module)}; and these functions
  681. share their methods in an interesting and dynamic way.
  682. To explain, let's call the imported generic functions (in @code{(math
  683. 2D-vectors)} and @code{(math 3D-vectors)}) the @dfn{ancestors}, and the
  684. merged generic function (in @code{(my-module)}), the @dfn{descendant}.
  685. The general rule is that for any generic function G, the applicable
  686. methods are selected from the union of the methods of G's descendant
  687. functions, the methods of G itself and the methods of G's ancestor
  688. functions.
  689. Thus ancestor functions effectively share methods with their
  690. descendants, and vice versa. In the example above, @code{x} in
  691. @code{(math 2D-vectors)} will share the methods of @code{x} in
  692. @code{(my-module)} and vice versa.@footnote{But note that @code{x} in
  693. @code{(math 2D-vectors)} doesn't share methods with @code{x} in
  694. @code{(math 3D-vectors)}, so modularity is still preserved.} Sharing is
  695. dynamic, so adding another new method to a descendant implies adding it
  696. to that descendant's ancestors too.
  697. @node Next-method
  698. @subsection Next-method
  699. When you call a generic function, with a particular set of arguments,
  700. GOOPS builds a list of all the methods that are applicable to those
  701. arguments and orders them by how closely the method definitions match
  702. the actual argument types. It then calls the method at the top of this
  703. list. If the selected method's code wants to call on to the next method
  704. in this list, it can do so by using @code{next-method}.
  705. @lisp
  706. (define-method (Test (a <integer>)) (cons 'integer (next-method)))
  707. (define-method (Test (a <number>)) (cons 'number (next-method)))
  708. (define-method (Test a) (list 'top))
  709. @end lisp
  710. With these definitions,
  711. @lisp
  712. (Test 1) @result{} (integer number top)
  713. (Test 1.0) @result{} (number top)
  714. (Test #t) @result{} (top)
  715. @end lisp
  716. @code{next-method} is always called as just @code{(next-method)}. The
  717. arguments for the next method call are always implicit, and always the
  718. same as for the original method call.
  719. If you want to call on to a method with the same name but with a
  720. different set of arguments (as you might with overloaded methods in C++,
  721. for example), you do not use @code{next-method}, but instead simply
  722. write the new call as usual:
  723. @lisp
  724. (define-method (Test (a <number>) min max)
  725. (if (and (>= a min) (<= a max))
  726. (display "Number is in range\n"))
  727. (Test a))
  728. (Test 2 1 10)
  729. @print{}
  730. Number is in range
  731. @result{}
  732. (integer number top)
  733. @end lisp
  734. (You should be careful in this case that the @code{Test} calls do not
  735. lead to an infinite recursion, but this consideration is just the same
  736. as in Scheme code in general.)
  737. @node Generic Function and Method Examples
  738. @subsection Generic Function and Method Examples
  739. Consider the following definitions:
  740. @lisp
  741. (define-generic G)
  742. (define-method (G (a <integer>) b) 'integer)
  743. (define-method (G (a <real>) b) 'real)
  744. (define-method (G a b) 'top)
  745. @end lisp
  746. The @code{define-generic} call defines @var{G} as a generic function.
  747. The three next lines define methods for @var{G}. Each method uses a
  748. sequence of @dfn{parameter specializers} that specify when the given
  749. method is applicable. A specializer permits to indicate the class a
  750. parameter must belong to (directly or indirectly) to be applicable. If
  751. no specializer is given, the system defaults it to @code{<top>}. Thus,
  752. the first method definition is equivalent to
  753. @cindex parameter specializers
  754. @lisp
  755. (define-method (G (a <integer>) (b <top>)) 'integer)
  756. @end lisp
  757. Now, let's look at some possible calls to the generic function @var{G}:
  758. @lisp
  759. (G 2 3) @result{} integer
  760. (G 2 #t) @result{} integer
  761. (G 1.2 'a) @result{} real
  762. @c (G #3 'a) @result{} real @c was {\sharpsign}
  763. (G #t #f) @result{} top
  764. (G 1 2 3) @result{} error (since no method exists for 3 parameters)
  765. @end lisp
  766. The methods above use only one specializer per parameter list. But in
  767. general, any or all of a method's parameters may be specialized.
  768. Suppose we define now:
  769. @lisp
  770. (define-method (G (a <integer>) (b <number>)) 'integer-number)
  771. (define-method (G (a <integer>) (b <real>)) 'integer-real)
  772. (define-method (G (a <integer>) (b <integer>)) 'integer-integer)
  773. (define-method (G a (b <number>)) 'top-number)
  774. @end lisp
  775. @noindent With these definitions:
  776. @lisp
  777. (G 1 2) @result{} integer-integer
  778. (G 1 1.0) @result{} integer-real
  779. (G 1 #t) @result{} integer
  780. (G 'a 1) @result{} top-number
  781. @end lisp
  782. As a further example we shall continue to define operations on the
  783. @code{<my-complex>} class. Suppose that we want to use it to implement
  784. complex numbers completely. For instance a definition for the addition
  785. of two complex numbers could be
  786. @lisp
  787. (define-method (new-+ (a <my-complex>) (b <my-complex>))
  788. (make-rectangular (+ (real-part a) (real-part b))
  789. (+ (imag-part a) (imag-part b))))
  790. @end lisp
  791. To be sure that the @code{+} used in the method @code{new-+} is the
  792. standard addition we can do:
  793. @lisp
  794. (define-generic new-+)
  795. (let ((+ +))
  796. (define-method (new-+ (a <my-complex>) (b <my-complex>))
  797. (make-rectangular (+ (real-part a) (real-part b))
  798. (+ (imag-part a) (imag-part b)))))
  799. @end lisp
  800. The @code{define-generic} ensures here that @code{new-+} will be defined
  801. in the global environment. Once this is done, we can add methods to the
  802. generic function @code{new-+} which make a closure on the @code{+}
  803. symbol. A complete writing of the @code{new-+} methods is shown in
  804. @ref{fig:newplus}.
  805. @float Figure,fig:newplus
  806. @lisp
  807. (define-generic new-+)
  808. (let ((+ +))
  809. (define-method (new-+ (a <real>) (b <real>)) (+ a b))
  810. (define-method (new-+ (a <real>) (b <my-complex>))
  811. (make-rectangular (+ a (real-part b)) (imag-part b)))
  812. (define-method (new-+ (a <my-complex>) (b <real>))
  813. (make-rectangular (+ (real-part a) b) (imag-part a)))
  814. (define-method (new-+ (a <my-complex>) (b <my-complex>))
  815. (make-rectangular (+ (real-part a) (real-part b))
  816. (+ (imag-part a) (imag-part b))))
  817. (define-method (new-+ (a <number>)) a)
  818. (define-method (new-+) 0)
  819. (define-method (new-+ . args)
  820. (new-+ (car args)
  821. (apply new-+ (cdr args)))))
  822. (set! + new-+)
  823. @end lisp
  824. @caption{Extending @code{+} to handle complex numbers}
  825. @end float
  826. We take advantage here of the fact that generic function are not obliged
  827. to have a fixed number of parameters. The four first methods implement
  828. dyadic addition. The fifth method says that the addition of a single
  829. element is this element itself. The sixth method says that using the
  830. addition with no parameter always return 0 (as is also true for the
  831. primitive @code{+}). The last method takes an arbitrary number of
  832. parameters@footnote{The parameter list for a @code{define-method}
  833. follows the conventions used for Scheme procedures. In particular it can
  834. use the dot notation or a symbol to denote an arbitrary number of
  835. parameters}. This method acts as a kind of @code{reduce}: it calls the
  836. dyadic addition on the @emph{car} of the list and on the result of
  837. applying it on its rest. To finish, the @code{set!} permits to redefine
  838. the @code{+} symbol to our extended addition.
  839. To conclude our implementation (integration?) of complex numbers, we
  840. could redefine standard Scheme predicates in the following manner:
  841. @lisp
  842. (define-method (complex? c <my-complex>) #t)
  843. (define-method (complex? c) #f)
  844. (define-method (number? n <number>) #t)
  845. (define-method (number? n) #f)
  846. @dots{}
  847. @end lisp
  848. Standard primitives in which complex numbers are involved could also be
  849. redefined in the same manner.
  850. @node Handling Invocation Errors
  851. @subsection Handling Invocation Errors
  852. If a generic function is invoked with a combination of parameters for
  853. which there is no applicable method, GOOPS raises an error.
  854. @deffn generic no-method
  855. @deffnx method no-method (gf <generic>) args
  856. When an application invokes a generic function, and no methods at all
  857. have been defined for that generic function, GOOPS calls the
  858. @code{no-method} generic function. The default method calls
  859. @code{goops-error} with an appropriate message.
  860. @end deffn
  861. @deffn generic no-applicable-method
  862. @deffnx method no-applicable-method (gf <generic>) args
  863. When an application applies a generic function to a set of arguments,
  864. and no methods have been defined for those argument types, GOOPS calls
  865. the @code{no-applicable-method} generic function. The default method
  866. calls @code{goops-error} with an appropriate message.
  867. @end deffn
  868. @deffn generic no-next-method
  869. @deffnx method no-next-method (gf <generic>) args
  870. When a generic function method calls @code{(next-method)} to invoke the
  871. next less specialized method for that generic function, and no less
  872. specialized methods have been defined for the current generic function
  873. arguments, GOOPS calls the @code{no-next-method} generic function. The
  874. default method calls @code{goops-error} with an appropriate message.
  875. @end deffn
  876. @node Inheritance
  877. @section Inheritance
  878. Here are some class definitions to help illustrate inheritance:
  879. @lisp
  880. (define-class A () a)
  881. (define-class B () b)
  882. (define-class C () c)
  883. (define-class D (A B) d a)
  884. (define-class E (A C) e c)
  885. (define-class F (D E) f)
  886. @end lisp
  887. @code{A}, @code{B}, @code{C} have a null list of superclasses. In this
  888. case, the system will replace the null list by a list which only
  889. contains @code{<object>}, the root of all the classes defined by
  890. @code{define-class}. @code{D}, @code{E}, @code{F} use multiple
  891. inheritance: each class inherits from two previously defined classes.
  892. Those class definitions define a hierarchy which is shown in
  893. @ref{fig:hier}. In this figure, the class @code{<top>} is also shown;
  894. this class is the superclass of all Scheme objects. In particular,
  895. @code{<top>} is the superclass of all standard Scheme
  896. types.
  897. @float Figure,fig:hier
  898. @iftex
  899. @center @image{hierarchy,5in}
  900. @end iftex
  901. @ifnottex
  902. @verbatiminclude hierarchy.txt
  903. @end ifnottex
  904. @caption{A class hierarchy.}
  905. @end float
  906. When a class has superclasses, its set of slots is calculated by taking
  907. the union of its own slots and those of all its superclasses. Thus each
  908. instance of D will have three slots, @code{a}, @code{b} and
  909. @code{d}). The slots of a class can be discovered using the
  910. @code{class-slots} primitive. For instance,
  911. @lisp
  912. (class-slots A) @result{} ((a))
  913. (class-slots E) @result{} ((a) (e) (c))
  914. (class-slots F) @result{} ((e) (c) (b) (d) (a) (f))
  915. @end lisp
  916. @noindent
  917. The ordering of the returned slots is not significant.
  918. @menu
  919. * Class Precedence List::
  920. * Sorting Methods::
  921. @end menu
  922. @node Class Precedence List
  923. @subsection Class Precedence List
  924. What happens when a class inherits from two or more superclasses that
  925. have a slot with the same name but incompatible definitions --- for
  926. example, different init values or slot allocations? We need a rule for
  927. deciding which slot definition the derived class ends up with, and this
  928. rule is provided by the class's @dfn{Class Precedence
  929. List}.@footnote{This section is an adaptation of material from Jeff
  930. Dalton's (J.Dalton@@ed.ac.uk) @cite{Brief introduction to CLOS}}
  931. Another problem arises when invoking a generic function, and there is
  932. more than one method that could apply to the call arguments. Here we
  933. need a way of ordering the applicable methods, so that Guile knows which
  934. method to use first, which to use next if that method calls
  935. @code{next-method}, and so on. One of the ingredients for this ordering
  936. is determining, for each given call argument, which of the specializing
  937. classes, from each applicable method's definition, is the most specific
  938. for that argument; and here again the class precedence list helps.
  939. If inheritance was restricted such that each class could only have one
  940. superclass --- which is known as @dfn{single} inheritance --- class
  941. ordering would be easy. The rule would be simply that a subclass is
  942. considered more specific than its superclass.
  943. With multiple inheritance, ordering is less obvious, and we have to
  944. impose an arbitrary rule to determine precedence. Suppose we have
  945. @lisp
  946. (define-class X ()
  947. (x #:init-value 1))
  948. (define-class Y ()
  949. (x #:init-value 2))
  950. (define-class Z (X Y)
  951. (@dots{}))
  952. @end lisp
  953. @noindent
  954. Clearly the @code{Z} class is more specific than @code{X} or @code{Y},
  955. for instances of @code{Z}. But which is more specific out of @code{X}
  956. and @code{Y} --- and hence, for the definitions above, which
  957. @code{#:init-value} will take effect when creating an instance of
  958. @code{Z}? The rule in @goops{} is that the superclasses listed earlier
  959. are more specific than those listed later. Hence @code{X} is more
  960. specific than @code{Y}, and the @code{#:init-value} for slot @code{x} in
  961. instances of @code{Z} will be 1.
  962. Hence there is a linear ordering for a class and all its
  963. superclasses, from most specific to least specific, and this ordering is
  964. called the Class Precedence List of the class.
  965. In fact the rules above are not quite enough to always determine a
  966. unique order, but they give an idea of how things work. For example,
  967. for the @code{F} class shown in @ref{fig:hier}, the class precedence
  968. list is
  969. @example
  970. (f d e a c b <object> <top>)
  971. @end example
  972. @noindent
  973. In cases where there is any ambiguity (like this one), it is a bad idea
  974. for programmers to rely on exactly what the order is. If the order for
  975. some superclasses is important, it can be expressed directly in the
  976. class definition.
  977. The precedence list of a class can be obtained by calling
  978. @code{class-precedence-list}. This function returns a ordered list
  979. whose first element is the most specific class. For instance:
  980. @lisp
  981. (class-precedence-list B) @result{} (#<<class> B 401b97c8>
  982. #<<class> <object> 401e4a10>
  983. #<<class> <top> 4026a9d8>)
  984. @end lisp
  985. @noindent
  986. Or for a more immediately readable result:
  987. @lisp
  988. (map class-name (class-precedence-list B)) @result{} (B <object> <top>)
  989. @end lisp
  990. @node Sorting Methods
  991. @subsection Sorting Methods
  992. Now, with the idea of the class precedence list, we can state precisely
  993. how the possible methods are sorted when more than one of the methods of
  994. a generic function are applicable to the call arguments.
  995. The rules are that
  996. @itemize
  997. @item
  998. the applicable methods are sorted in order of specificity, and the most
  999. specific method is used first, then the next if that method calls
  1000. @code{next-method}, and so on
  1001. @item
  1002. a method M1 is more specific than another method M2 if the first
  1003. specializing class that differs, between the definitions of M1 and M2,
  1004. is more specific, in M1's definition, for the corresponding actual call
  1005. argument, than the specializing class in M2's definition
  1006. @item
  1007. a class C1 is more specific than another class C2, for an object of
  1008. actual class C, if C1 comes before C2 in C's class precedence list.
  1009. @end itemize
  1010. @node Introspection
  1011. @section Introspection
  1012. @dfn{Introspection}, or @dfn{reflection}, means being able to obtain
  1013. information dynamically about GOOPS objects. It is perhaps best
  1014. illustrated by considering an object oriented language that does not
  1015. provide any introspection, namely C++.
  1016. Nothing in C++ allows a running program to obtain answers to the following
  1017. types of question:
  1018. @itemize @bullet
  1019. @item
  1020. What are the data members of this object or class?
  1021. @item
  1022. What classes does this class inherit from?
  1023. @item
  1024. Is this method call virtual or non-virtual?
  1025. @item
  1026. If I invoke @code{Employee::adjustHoliday()}, what class contains the
  1027. @code{adjustHoliday()} method that will be applied?
  1028. @end itemize
  1029. In C++, answers to such questions can only be determined by looking at
  1030. the source code, if you have access to it. GOOPS, on the other hand,
  1031. includes procedures that allow answers to these questions --- or their
  1032. GOOPS equivalents --- to be obtained dynamically, at run time.
  1033. @menu
  1034. * Classes::
  1035. * Instances::
  1036. * Slots::
  1037. * Generic Functions::
  1038. * Accessing Slots::
  1039. @end menu
  1040. @node Classes
  1041. @subsection Classes
  1042. A GOOPS class is itself an instance of the @code{<class>} class, or of a
  1043. subclass of @code{<class>}. The definition of the @code{<class>} class
  1044. has slots that are used to describe the properties of a class, including
  1045. the following.
  1046. @deffn {primitive procedure} class-name class
  1047. Return the name of class @var{class}. This is the value of
  1048. @var{class}'s @code{name} slot.
  1049. @end deffn
  1050. @deffn {primitive procedure} class-direct-supers class
  1051. Return a list containing the direct superclasses of @var{class}. This
  1052. is the value of @var{class}'s @code{direct-supers} slot.
  1053. @end deffn
  1054. @deffn {primitive procedure} class-direct-slots class
  1055. Return a list containing the slot definitions of the direct slots of
  1056. @var{class}. This is the value of @var{class}'s @code{direct-slots}
  1057. slot.
  1058. @end deffn
  1059. @deffn {primitive procedure} class-direct-subclasses class
  1060. Return a list containing the direct subclasses of @var{class}. This is
  1061. the value of @var{class}'s @code{direct-subclasses} slot.
  1062. @end deffn
  1063. @deffn {primitive procedure} class-direct-methods class
  1064. Return a list of all the generic function methods that use @var{class}
  1065. as a formal parameter specializer. This is the value of @var{class}'s
  1066. @code{direct-methods} slot.
  1067. @end deffn
  1068. @deffn {primitive procedure} class-precedence-list class
  1069. Return the class precedence list for class @var{class} (@pxref{Class
  1070. Precedence List}). This is the value of @var{class}'s @code{cpl} slot.
  1071. @end deffn
  1072. @deffn {primitive procedure} class-slots class
  1073. Return a list containing the slot definitions for all @var{class}'s
  1074. slots, including any slots that are inherited from superclasses. This
  1075. is the value of @var{class}'s @code{slots} slot.
  1076. @end deffn
  1077. @deffn procedure class-subclasses class
  1078. Return a list of all subclasses of @var{class}.
  1079. @end deffn
  1080. @deffn procedure class-methods class
  1081. Return a list of all methods that use @var{class} or a subclass of
  1082. @var{class} as one of its formal parameter specializers.
  1083. @end deffn
  1084. @node Instances
  1085. @subsection Instances
  1086. @deffn {primitive procedure} class-of value
  1087. Return the GOOPS class of any Scheme @var{value}.
  1088. @end deffn
  1089. @deffn {primitive procedure} instance? object
  1090. Return @code{#t} if @var{object} is any GOOPS instance, otherwise
  1091. @code{#f}.
  1092. @end deffn
  1093. @deffn procedure is-a? object class
  1094. Return @code{#t} if @var{object} is an instance of @var{class} or one of
  1095. its subclasses.
  1096. @end deffn
  1097. You can use the @code{is-a?} predicate to ask whether any given value
  1098. belongs to a given class, or @code{class-of} to discover the class of a
  1099. given value. Note that when GOOPS is loaded (by code using the
  1100. @code{(oop goops)} module) built-in classes like @code{<string>},
  1101. @code{<list>} and @code{<number>} are automatically set up,
  1102. corresponding to all Guile Scheme types.
  1103. @lisp
  1104. (is-a? 2.3 <number>) @result{} #t
  1105. (is-a? 2.3 <real>) @result{} #t
  1106. (is-a? 2.3 <string>) @result{} #f
  1107. (is-a? '("a" "b") <string>) @result{} #f
  1108. (is-a? '("a" "b") <list>) @result{} #t
  1109. (is-a? (car '("a" "b")) <string>) @result{} #t
  1110. (is-a? <string> <class>) @result{} #t
  1111. (is-a? <class> <string>) @result{} #f
  1112. (class-of 2.3) @result{} #<<class> <real> 908c708>
  1113. (class-of #(1 2 3)) @result{} #<<class> <vector> 908cd20>
  1114. (class-of <string>) @result{} #<<class> <class> 8bd3e10>
  1115. (class-of <class>) @result{} #<<class> <class> 8bd3e10>
  1116. @end lisp
  1117. @node Slots
  1118. @subsection Slots
  1119. @deffn procedure class-slot-definition class slot-name
  1120. Return the slot definition for the slot named @var{slot-name} in class
  1121. @var{class}. @var{slot-name} should be a symbol.
  1122. @end deffn
  1123. @deffn procedure slot-definition-name slot-def
  1124. Extract and return the slot name from @var{slot-def}.
  1125. @end deffn
  1126. @deffn procedure slot-definition-options slot-def
  1127. Extract and return the slot options from @var{slot-def}.
  1128. @end deffn
  1129. @deffn procedure slot-definition-allocation slot-def
  1130. Extract and return the slot allocation option from @var{slot-def}. This
  1131. is the value of the @code{#:allocation} keyword (@pxref{Slot Options,,
  1132. allocation}), or @code{#:instance} if the @code{#:allocation} keyword is
  1133. absent.
  1134. @end deffn
  1135. @deffn procedure slot-definition-getter slot-def
  1136. Extract and return the slot getter option from @var{slot-def}. This is
  1137. the value of the @code{#:getter} keyword (@pxref{Slot Options,,
  1138. getter}), or @code{#f} if the @code{#:getter} keyword is absent.
  1139. @end deffn
  1140. @deffn procedure slot-definition-setter slot-def
  1141. Extract and return the slot setter option from @var{slot-def}. This is
  1142. the value of the @code{#:setter} keyword (@pxref{Slot Options,,
  1143. setter}), or @code{#f} if the @code{#:setter} keyword is absent.
  1144. @end deffn
  1145. @deffn procedure slot-definition-accessor slot-def
  1146. Extract and return the slot accessor option from @var{slot-def}. This
  1147. is the value of the @code{#:accessor} keyword (@pxref{Slot Options,,
  1148. accessor}), or @code{#f} if the @code{#:accessor} keyword is absent.
  1149. @end deffn
  1150. @deffn procedure slot-definition-init-value slot-def
  1151. Extract and return the slot init-value option from @var{slot-def}. This
  1152. is the value of the @code{#:init-value} keyword (@pxref{Slot Options,,
  1153. init-value}), or the unbound value if the @code{#:init-value} keyword is
  1154. absent.
  1155. @end deffn
  1156. @deffn procedure slot-definition-init-form slot-def
  1157. Extract and return the slot init-form option from @var{slot-def}. This
  1158. is the value of the @code{#:init-form} keyword (@pxref{Slot Options,,
  1159. init-form}), or the unbound value if the @code{#:init-form} keyword is
  1160. absent.
  1161. @end deffn
  1162. @deffn procedure slot-definition-init-thunk slot-def
  1163. Extract and return the slot init-thunk option from @var{slot-def}. This
  1164. is the value of the @code{#:init-thunk} keyword (@pxref{Slot Options,,
  1165. init-thunk}), or @code{#f} if the @code{#:init-thunk} keyword is absent.
  1166. @end deffn
  1167. @deffn procedure slot-definition-init-keyword slot-def
  1168. Extract and return the slot init-keyword option from @var{slot-def}.
  1169. This is the value of the @code{#:init-keyword} keyword (@pxref{Slot
  1170. Options,, init-keyword}), or @code{#f} if the @code{#:init-keyword}
  1171. keyword is absent.
  1172. @end deffn
  1173. @deffn procedure slot-init-function class slot-name
  1174. Return the initialization function for the slot named @var{slot-name} in
  1175. class @var{class}. @var{slot-name} should be a symbol.
  1176. The returned initialization function incorporates the effects of the
  1177. standard @code{#:init-thunk}, @code{#:init-form} and @code{#:init-value}
  1178. slot options. These initializations can be overridden by the
  1179. @code{#:init-keyword} slot option or by a specialized @code{initialize}
  1180. method, so, in general, the function returned by
  1181. @code{slot-init-function} may be irrelevant. For a fuller discussion,
  1182. see @ref{Slot Options,, init-value}.
  1183. @end deffn
  1184. @node Generic Functions
  1185. @subsection Generic Functions
  1186. A generic function is an instance of the @code{<generic>} class, or of a
  1187. subclass of @code{<generic>}. The definition of the @code{<generic>}
  1188. class has slots that are used to describe the properties of a generic
  1189. function.
  1190. @deffn {primitive procedure} generic-function-name gf
  1191. Return the name of generic function @var{gf}.
  1192. @end deffn
  1193. @deffn {primitive procedure} generic-function-methods gf
  1194. Return a list of the methods of generic function @var{gf}. This is the
  1195. value of @var{gf}'s @code{methods} slot.
  1196. @end deffn
  1197. Similarly, a method is an instance of the @code{<method>} class, or of a
  1198. subclass of @code{<method>}; and the definition of the @code{<method>}
  1199. class has slots that are used to describe the properties of a method.
  1200. @deffn {primitive procedure} method-generic-function method
  1201. Return the generic function that @var{method} belongs to. This is the
  1202. value of @var{method}'s @code{generic-function} slot.
  1203. @end deffn
  1204. @deffn {primitive procedure} method-specializers method
  1205. Return a list of @var{method}'s formal parameter specializers . This is
  1206. the value of @var{method}'s @code{specializers} slot.
  1207. @end deffn
  1208. @deffn {primitive procedure} method-procedure method
  1209. Return the procedure that implements @var{method}. This is the value of
  1210. @var{method}'s @code{procedure} slot.
  1211. @end deffn
  1212. @deffn generic method-source
  1213. @deffnx method method-source (m <method>)
  1214. Return an expression that prints to show the definition of method
  1215. @var{m}.
  1216. @example
  1217. (define-generic cube)
  1218. (define-method (cube (n <number>))
  1219. (* n n n))
  1220. (map method-source (generic-function-methods cube))
  1221. @result{}
  1222. ((method ((n <number>)) (* n n n)))
  1223. @end example
  1224. @end deffn
  1225. @node Accessing Slots
  1226. @subsection Accessing Slots
  1227. Any slot, regardless of its allocation, can be queried, referenced and
  1228. set using the following four primitive procedures.
  1229. @deffn {primitive procedure} slot-exists? obj slot-name
  1230. Return @code{#t} if @var{obj} has a slot with name @var{slot-name},
  1231. otherwise @code{#f}.
  1232. @end deffn
  1233. @deffn {primitive procedure} slot-bound? obj slot-name
  1234. Return @code{#t} if the slot named @var{slot-name} in @var{obj} has a
  1235. value, otherwise @code{#f}.
  1236. @code{slot-bound?} calls the generic function @code{slot-missing} if
  1237. @var{obj} does not have a slot called @var{slot-name} (@pxref{Accessing
  1238. Slots, slot-missing}).
  1239. @end deffn
  1240. @deffn {primitive procedure} slot-ref obj slot-name
  1241. Return the value of the slot named @var{slot-name} in @var{obj}.
  1242. @code{slot-ref} calls the generic function @code{slot-missing} if
  1243. @var{obj} does not have a slot called @var{slot-name} (@pxref{Accessing
  1244. Slots, slot-missing}).
  1245. @code{slot-ref} calls the generic function @code{slot-unbound} if the
  1246. named slot in @var{obj} does not have a value (@pxref{Accessing Slots,
  1247. slot-unbound}).
  1248. @end deffn
  1249. @deffn {primitive procedure} slot-set! obj slot-name value
  1250. Set the value of the slot named @var{slot-name} in @var{obj} to @var{value}.
  1251. @code{slot-set!} calls the generic function @code{slot-missing} if
  1252. @var{obj} does not have a slot called @var{slot-name} (@pxref{Accessing
  1253. Slots, slot-missing}).
  1254. @end deffn
  1255. GOOPS stores information about slots in classes. Internally,
  1256. all of these procedures work by looking up the slot definition for the
  1257. slot named @var{slot-name} in the class @code{(class-of
  1258. @var{obj})}, and then using the slot definition's ``getter'' and
  1259. ``setter'' closures to get and set the slot value.
  1260. The next four procedures differ from the previous ones in that they take
  1261. the class as an explicit argument, rather than assuming
  1262. @code{(class-of @var{obj})}. Therefore they allow you to apply the
  1263. ``getter'' and ``setter'' closures of a slot definition in one class to
  1264. an instance of a different class.
  1265. @deffn {primitive procedure} slot-exists-using-class? class obj slot-name
  1266. Return @code{#t} if @var{class} has a slot definition for a slot with
  1267. name @var{slot-name}, otherwise @code{#f}.
  1268. @end deffn
  1269. @deffn {primitive procedure} slot-bound-using-class? class obj slot-name
  1270. Return @code{#t} if applying @code{slot-ref-using-class} to the same
  1271. arguments would call the generic function @code{slot-unbound}, otherwise
  1272. @code{#f}.
  1273. @code{slot-bound-using-class?} calls the generic function
  1274. @code{slot-missing} if @var{class} does not have a slot definition for a
  1275. slot called @var{slot-name} (@pxref{Accessing Slots,
  1276. slot-missing}).
  1277. @end deffn
  1278. @deffn {primitive procedure} slot-ref-using-class class obj slot-name
  1279. Apply the ``getter'' closure for the slot named @var{slot-name} in
  1280. @var{class} to @var{obj}, and return its result.
  1281. @code{slot-ref-using-class} calls the generic function
  1282. @code{slot-missing} if @var{class} does not have a slot definition for a
  1283. slot called @var{slot-name} (@pxref{Accessing Slots,
  1284. slot-missing}).
  1285. @code{slot-ref-using-class} calls the generic function
  1286. @code{slot-unbound} if the application of the ``getter'' closure to
  1287. @var{obj} returns an unbound value (@pxref{Accessing Slots,
  1288. slot-unbound}).
  1289. @end deffn
  1290. @deffn {primitive procedure} slot-set-using-class! class obj slot-name value
  1291. Apply the ``setter'' closure for the slot named @var{slot-name} in
  1292. @var{class} to @var{obj} and @var{value}.
  1293. @code{slot-set-using-class!} calls the generic function
  1294. @code{slot-missing} if @var{class} does not have a slot definition for a
  1295. slot called @var{slot-name} (@pxref{Accessing Slots, slot-missing}).
  1296. @end deffn
  1297. Slots whose allocation is per-class rather than per-instance can be
  1298. referenced and set without needing to specify any particular instance.
  1299. @deffn procedure class-slot-ref class slot-name
  1300. Return the value of the slot named @var{slot-name} in class @var{class}.
  1301. The named slot must have @code{#:class} or @code{#:each-subclass}
  1302. allocation (@pxref{Slot Options,, allocation}).
  1303. If there is no such slot with @code{#:class} or @code{#:each-subclass}
  1304. allocation, @code{class-slot-ref} calls the @code{slot-missing} generic
  1305. function with arguments @var{class} and @var{slot-name}. Otherwise, if
  1306. the slot value is unbound, @code{class-slot-ref} calls the
  1307. @code{slot-unbound} generic function, with the same arguments.
  1308. @end deffn
  1309. @deffn procedure class-slot-set! class slot-name value
  1310. Set the value of the slot named @var{slot-name} in class @var{class} to
  1311. @var{value}. The named slot must have @code{#:class} or
  1312. @code{#:each-subclass} allocation (@pxref{Slot Options,, allocation}).
  1313. If there is no such slot with @code{#:class} or @code{#:each-subclass}
  1314. allocation, @code{class-slot-ref} calls the @code{slot-missing} generic
  1315. function with arguments @var{class} and @var{slot-name}.
  1316. @end deffn
  1317. When a @code{slot-ref} or @code{slot-set!} call specifies a non-existent
  1318. slot name, or tries to reference a slot whose value is unbound, GOOPS
  1319. calls one of the following generic functions.
  1320. @deffn generic slot-missing
  1321. @deffnx method slot-missing (class <class>) slot-name
  1322. @deffnx method slot-missing (class <class>) (object <object>) slot-name
  1323. @deffnx method slot-missing (class <class>) (object <object>) slot-name value
  1324. When an application attempts to reference or set a class or instance
  1325. slot by name, and the slot name is invalid for the specified @var{class}
  1326. or @var{object}, GOOPS calls the @code{slot-missing} generic function.
  1327. The default methods all call @code{goops-error} with an appropriate
  1328. message.
  1329. @end deffn
  1330. @deffn generic slot-unbound
  1331. @deffnx method slot-unbound (object <object>)
  1332. @deffnx method slot-unbound (class <class>) slot-name
  1333. @deffnx method slot-unbound (class <class>) (object <object>) slot-name
  1334. When an application attempts to reference a class or instance slot, and
  1335. the slot's value is unbound, GOOPS calls the @code{slot-unbound} generic
  1336. function.
  1337. The default methods all call @code{goops-error} with an appropriate
  1338. message.
  1339. @end deffn
  1340. @node GOOPS Error Handling
  1341. @section Error Handling
  1342. The procedure @code{goops-error} is called to raise an appropriate error
  1343. by the default methods of the following generic functions:
  1344. @itemize @bullet
  1345. @item
  1346. @code{slot-missing} (@pxref{Accessing Slots,, slot-missing})
  1347. @item
  1348. @code{slot-unbound} (@pxref{Accessing Slots,, slot-unbound})
  1349. @item
  1350. @code{no-method} (@pxref{Handling Invocation Errors,, no-method})
  1351. @item
  1352. @code{no-applicable-method} (@pxref{Handling Invocation Errors,,
  1353. no-applicable-method})
  1354. @item
  1355. @code{no-next-method} (@pxref{Handling Invocation Errors,,
  1356. no-next-method})
  1357. @end itemize
  1358. If you customize these functions for particular classes or metaclasses,
  1359. you may still want to use @code{goops-error} to signal any error
  1360. conditions that you detect.
  1361. @deffn procedure goops-error format-string arg @dots{}
  1362. Raise an error with key @code{goops-error} and error message constructed
  1363. from @var{format-string} and @var{arg} @enddots{}. Error message
  1364. formatting is as done by @code{scm-error}.
  1365. @end deffn
  1366. @node GOOPS Object Miscellany
  1367. @section GOOPS Object Miscellany
  1368. Here we cover some points about GOOPS objects that aren't substantial
  1369. enough to merit sections on their own.
  1370. @subheading Object Equality
  1371. When GOOPS is loaded, @code{eqv?}, @code{equal?} and @code{=} become
  1372. generic functions, and you can define methods for them, specialized for
  1373. your own classes, so as to control what the various kinds of equality
  1374. mean for your classes.
  1375. For example, the @code{assoc} procedure, for looking up an entry in an
  1376. alist, is specified as using @code{equal?} to determine when the car of
  1377. an entry in the alist is the same as the key parameter that @code{assoc}
  1378. is called with. Hence, if you had defined a new class, and wanted to
  1379. use instances of that class as the keys in an alist, you could define a
  1380. method for @code{equal?}, for your class, to control @code{assoc}'s
  1381. lookup precisely.
  1382. @subheading Cloning Objects
  1383. @deffn generic shallow-clone
  1384. @deffnx method shallow-clone (self <object>)
  1385. Return a ``shallow'' clone of @var{self}. The default method makes a
  1386. shallow clone by allocating a new instance and copying slot values from
  1387. self to the new instance. Each slot value is copied either as an
  1388. immediate value or by reference.
  1389. @end deffn
  1390. @deffn generic deep-clone
  1391. @deffnx method deep-clone (self <object>)
  1392. Return a ``deep'' clone of @var{self}. The default method makes a deep
  1393. clone by allocating a new instance and copying or cloning slot values
  1394. from self to the new instance. If a slot value is an instance
  1395. (satisfies @code{instance?}), it is cloned by calling @code{deep-clone}
  1396. on that value. Other slot values are copied either as immediate values
  1397. or by reference.
  1398. @end deffn
  1399. @subheading Write and Display
  1400. @deffn {primitive generic} write object port
  1401. @deffnx {primitive generic} display object port
  1402. When GOOPS is loaded, @code{write} and @code{display} become generic
  1403. functions with special methods for printing
  1404. @itemize @bullet
  1405. @item
  1406. objects - instances of the class @code{<object>}
  1407. @item
  1408. foreign objects - instances of the class @code{<foreign-object>}
  1409. @item
  1410. classes - instances of the class @code{<class>}
  1411. @item
  1412. generic functions - instances of the class @code{<generic>}
  1413. @item
  1414. methods - instances of the class @code{<method>}.
  1415. @end itemize
  1416. @code{write} and @code{display} print non-GOOPS values in the same way
  1417. as the Guile primitive @code{write} and @code{display} functions.
  1418. @end deffn
  1419. In addition to the cases mentioned, you can of course define
  1420. @code{write} and @code{display} methods for your own classes, to
  1421. customize how instances of those classes are printed.
  1422. @node The Metaobject Protocol
  1423. @section The Metaobject Protocol
  1424. At this point, we've said about as much as can be said about GOOPS
  1425. without having to confront the idea of the metaobject protocol. There
  1426. are a couple more topics that could be discussed in isolation first ---
  1427. class redefinition, and changing the class of existing instances --- but
  1428. in practice developers using them will be advanced enough to want to
  1429. understand the metaobject protocol too, and will probably be using the
  1430. protocol to customize exactly what happens during these events.
  1431. So let's plunge in. GOOPS is based on a ``metaobject protocol'' (aka
  1432. ``MOP'') derived from the ones used in CLOS (the Common Lisp Object
  1433. System), tiny-clos (a small Scheme implementation of a subset of CLOS
  1434. functionality) and STKlos.
  1435. The MOP underlies many possible GOOPS customizations --- such as
  1436. defining an @code{initialize} method to customize the initialization of
  1437. instances of an application-defined class --- and an understanding of
  1438. the MOP makes it much easier to explain such customizations in a precise
  1439. way. And at a deeper level, understanding the MOP is a key part of
  1440. understanding GOOPS, and of taking full advantage of GOOPS' power, by
  1441. customizing the behaviour of GOOPS itself.
  1442. @menu
  1443. * Metaobjects and the Metaobject Protocol::
  1444. * Metaclasses::
  1445. * MOP Specification::
  1446. * Instance Creation Protocol::
  1447. * Class Definition Protocol::
  1448. * Customizing Class Definition::
  1449. * Method Definition::
  1450. * Method Definition Internals::
  1451. * Generic Function Internals::
  1452. * Generic Function Invocation::
  1453. @end menu
  1454. @node Metaobjects and the Metaobject Protocol
  1455. @subsection Metaobjects and the Metaobject Protocol
  1456. The building blocks of GOOPS are classes, slot definitions, instances,
  1457. generic functions and methods. A class is a grouping of inheritance
  1458. relations and slot definitions. An instance is an object with slots
  1459. that are allocated following the rules implied by its class's
  1460. superclasses and slot definitions. A generic function is a collection
  1461. of methods and rules for determining which of those methods to apply
  1462. when the generic function is invoked. A method is a procedure and a set
  1463. of specializers that specify the type of arguments to which the
  1464. procedure is applicable.
  1465. Of these entities, GOOPS represents classes, generic functions and
  1466. methods as ``metaobjects''. In other words, the values in a GOOPS
  1467. program that describe classes, generic functions and methods, are
  1468. themselves instances (or ``objects'') of special GOOPS classes that
  1469. encapsulate the behaviour, respectively, of classes, generic functions,
  1470. and methods.
  1471. (The other two entities are slot definitions and instances. Slot
  1472. definitions are not strictly instances, but every slot definition is
  1473. associated with a GOOPS class that specifies the behaviour of the slot
  1474. as regards accessibility and protection from garbage collection.
  1475. Instances are of course objects in the usual sense, and there is no
  1476. benefit from thinking of them as metaobjects.)
  1477. The ``metaobject protocol'' (or ``MOP'') is the specification of the
  1478. generic functions which determine the behaviour of these metaobjects and
  1479. the circumstances in which these generic functions are invoked.
  1480. For a concrete example of what this means, consider how GOOPS calculates
  1481. the set of slots for a class that is being defined using
  1482. @code{define-class}. The desired set of slots is the union of the new
  1483. class's direct slots and the slots of all its superclasses. But
  1484. @code{define-class} itself does not perform this calculation. Instead,
  1485. there is a method of the @code{initialize} generic function that is
  1486. specialized for instances of type @code{<class>}, and it is this method
  1487. that performs the slot calculation.
  1488. @code{initialize} is a generic function which GOOPS calls whenever a new
  1489. instance is created, immediately after allocating memory for a new
  1490. instance, in order to initialize the new instance's slots. The sequence
  1491. of steps is as follows.
  1492. @itemize @bullet
  1493. @item
  1494. @code{define-class} uses @code{make} to make a new instance of the
  1495. @code{<class>} class, passing as initialization arguments the
  1496. superclasses, slot definitions and class options that were specified in
  1497. the @code{define-class} form.
  1498. @item
  1499. @code{make} allocates memory for the new instance, and invokes the
  1500. @code{initialize} generic function to initialize the new instance's
  1501. slots.
  1502. @item
  1503. The @code{initialize} generic function applies the method that is
  1504. specialized for instances of type @code{<class>}, and this method
  1505. performs the slot calculation.
  1506. @end itemize
  1507. In other words, rather than being hardcoded in @code{define-class}, the
  1508. default behaviour of class definition is encapsulated by generic
  1509. function methods that are specialized for the class @code{<class>}.
  1510. It is possible to create a new class that inherits from @code{<class>},
  1511. which is called a ``metaclass'', and to write a new @code{initialize}
  1512. method that is specialized for instances of the new metaclass. Then, if
  1513. the @code{define-class} form includes a @code{#:metaclass} class option
  1514. whose value is the new metaclass, the class that is defined by the
  1515. @code{define-class} form will be an instance of the new metaclass rather
  1516. than of the default @code{<class>}, and will be defined in accordance
  1517. with the new @code{initialize} method. Thus the default slot
  1518. calculation, as well as any other aspect of the new class's relationship
  1519. with its superclasses, can be modified or overridden.
  1520. In a similar way, the behaviour of generic functions can be modified or
  1521. overridden by creating a new class that inherits from the standard
  1522. generic function class @code{<generic>}, writing appropriate methods
  1523. that are specialized to the new class, and creating new generic
  1524. functions that are instances of the new class.
  1525. The same is true for method metaobjects. And the same basic mechanism
  1526. allows the application class author to write an @code{initialize} method
  1527. that is specialized to their application class, to initialize instances
  1528. of that class.
  1529. Such is the power of the MOP. Note that @code{initialize} is just one
  1530. of a large number of generic functions that can be customized to modify
  1531. the behaviour of application objects and classes and of GOOPS itself.
  1532. Each following section covers a particular area of GOOPS functionality,
  1533. and describes the generic functions that are relevant for customization
  1534. of that area.
  1535. @node Metaclasses
  1536. @subsection Metaclasses
  1537. A @dfn{metaclass} is the class of an object which represents a GOOPS
  1538. class. Put more succinctly, a metaclass is a class's class.
  1539. Most GOOPS classes have the metaclass @code{<class>} and, by default,
  1540. any new class that is created using @code{define-class} has the
  1541. metaclass @code{<class>}.
  1542. But what does this really mean? To find out, let's look in more detail
  1543. at what happens when a new class is created using @code{define-class}:
  1544. @example
  1545. (define-class <my-class> (<object>) . slots)
  1546. @end example
  1547. @noindent
  1548. Guile expands this to something like:
  1549. @example
  1550. (define <my-class> (class (<object>) . slots))
  1551. @end example
  1552. @noindent
  1553. which in turn expands to:
  1554. @example
  1555. (define <my-class>
  1556. (make <class> #:dsupers (list <object>) #:slots slots))
  1557. @end example
  1558. As this expansion makes clear, the resulting value of @code{<my-class>}
  1559. is an instance of the class @code{<class>} with slot values specifying
  1560. the superclasses and slot definitions for the class @code{<my-class>}.
  1561. (@code{#:dsupers} and @code{#:slots} are initialization keywords for the
  1562. @code{dsupers} and @code{dslots} slots of the @code{<class>} class.)
  1563. Now suppose that you want to define a new class with a metaclass other
  1564. than the default @code{<class>}. This is done by writing:
  1565. @example
  1566. (define-class <my-class2> (<object>)
  1567. slot @dots{}
  1568. #:metaclass <my-metaclass>)
  1569. @end example
  1570. @noindent
  1571. and Guile expands @emph{this} to something like:
  1572. @example
  1573. (define <my-class2>
  1574. (make <my-metaclass> #:dsupers (list <object>) #:slots slots))
  1575. @end example
  1576. In this case, the value of @code{<my-class2>} is an instance of the more
  1577. specialized class @code{<my-metaclass>}. Note that
  1578. @code{<my-metaclass>} itself must previously have been defined as a
  1579. subclass of @code{<class>}. For a full discussion of when and how it is
  1580. useful to define new metaclasses, see @ref{MOP Specification}.
  1581. Now let's make an instance of @code{<my-class2>}:
  1582. @example
  1583. (define my-object (make <my-class2> ...))
  1584. @end example
  1585. All of the following statements are correct expressions of the
  1586. relationships between @code{my-object}, @code{<my-class2>},
  1587. @code{<my-metaclass>} and @code{<class>}.
  1588. @itemize @bullet
  1589. @item
  1590. @code{my-object} is an instance of the class @code{<my-class2>}.
  1591. @item
  1592. @code{<my-class2>} is an instance of the class @code{<my-metaclass>}.
  1593. @item
  1594. @code{<my-metaclass>} is an instance of the class @code{<class>}.
  1595. @item
  1596. The class of @code{my-object} is @code{<my-class2>}.
  1597. @item
  1598. The class of @code{<my-class2>} is @code{<my-metaclass>}.
  1599. @item
  1600. The class of @code{<my-metaclass>} is @code{<class>}.
  1601. @end itemize
  1602. @node MOP Specification
  1603. @subsection MOP Specification
  1604. The aim of the MOP specification in this chapter is to specify all the
  1605. customizable generic function invocations that can be made by the standard
  1606. GOOPS syntax, procedures and methods, and to explain the protocol for
  1607. customizing such invocations.
  1608. A generic function invocation is customizable if the types of the
  1609. arguments to which it is applied are not completely determined by the
  1610. lexical context in which the invocation appears. For example, the
  1611. @code{(initialize @var{instance} @var{initargs})} invocation in the
  1612. default @code{make-instance} method is customizable, because the type of
  1613. the @code{@var{instance}} argument is determined by the class that was
  1614. passed to @code{make-instance}.
  1615. (Whereas --- to give a counter-example --- the @code{(make <generic>
  1616. #:name ',name)} invocation in @code{define-generic} is not customizable,
  1617. because all of its arguments have lexically determined types.)
  1618. When using this rule to decide whether a given generic function invocation
  1619. is customizable, we ignore arguments that are expected to be handled in
  1620. method definitions as a single ``rest'' list argument.
  1621. For each customizable generic function invocation, the @dfn{invocation
  1622. protocol} is explained by specifying
  1623. @itemize @bullet
  1624. @item
  1625. what, conceptually, the applied method is intended to do
  1626. @item
  1627. what assumptions, if any, the caller makes about the applied method's side
  1628. effects
  1629. @item
  1630. what the caller expects to get as the applied method's return value.
  1631. @end itemize
  1632. @node Instance Creation Protocol
  1633. @subsection Instance Creation Protocol
  1634. @code{make <class> . @var{initargs}} (method)
  1635. @itemize @bullet
  1636. @item
  1637. @code{allocate-instance @var{class} @var{initargs}} (generic)
  1638. The applied @code{allocate-instance} method should allocate storage for
  1639. a new instance of class @var{class} and return the uninitialized instance.
  1640. @item
  1641. @code{initialize @var{instance} @var{initargs}} (generic)
  1642. @var{instance} is the uninitialized instance returned by
  1643. @code{allocate-instance}. The applied method should initialize the new
  1644. instance in whatever sense is appropriate for its class. The method's
  1645. return value is ignored.
  1646. @end itemize
  1647. @code{make} itself is a generic function. Hence the @code{make}
  1648. invocation itself can be customized in the case where the new instance's
  1649. metaclass is more specialized than the default @code{<class>}, by
  1650. defining a @code{make} method that is specialized to that metaclass.
  1651. Normally, however, the method for classes with metaclass @code{<class>}
  1652. will be applied. This method calls two generic functions:
  1653. @itemize @bullet
  1654. @item
  1655. (allocate-instance @var{class} . @var{initargs})
  1656. @item
  1657. (initialize @var{instance} . @var{initargs})
  1658. @end itemize
  1659. @code{allocate-instance} allocates storage for and returns the new
  1660. instance, uninitialized. You might customize @code{allocate-instance},
  1661. for example, if you wanted to provide a GOOPS wrapper around some other
  1662. object programming system.
  1663. To do this, you would create a specialized metaclass, which would act as
  1664. the metaclass for all classes and instances from the other system. Then
  1665. define an @code{allocate-instance} method, specialized to that
  1666. metaclass, which calls a Guile primitive C function (or FFI code), which
  1667. in turn allocates the new instance using the interface of the other
  1668. object system.
  1669. In this case, for a complete system, you would also need to customize a
  1670. number of other generic functions like @code{make} and
  1671. @code{initialize}, so that GOOPS knows how to make classes from the
  1672. other system, access instance slots, and so on.
  1673. @code{initialize} initializes the instance that is returned by
  1674. @code{allocate-instance}. The standard GOOPS methods perform
  1675. initializations appropriate to the instance class.
  1676. @itemize @bullet
  1677. @item
  1678. At the least specialized level, the method for instances of type
  1679. @code{<object>} performs internal GOOPS instance initialization, and
  1680. initializes the instance's slots according to the slot definitions and
  1681. any slot initialization keywords that appear in @var{initargs}.
  1682. @item
  1683. The method for instances of type @code{<class>} calls
  1684. @code{(next-method)}, then performs the class initializations described
  1685. in @ref{Class Definition Protocol}.
  1686. @item
  1687. and so on for generic functions, methods, operator classes @dots{}
  1688. @end itemize
  1689. Similarly, you can customize the initialization of instances of any
  1690. application-defined class by defining an @code{initialize} method
  1691. specialized to that class.
  1692. Imagine a class whose instances' slots need to be initialized at
  1693. instance creation time by querying a database. Although it might be
  1694. possible to achieve this a combination of @code{#:init-thunk} keywords
  1695. and closures in the slot definitions, it may be neater to write an
  1696. @code{initialize} method for the class that queries the database once
  1697. and initializes all the dependent slot values according to the results.
  1698. @node Class Definition Protocol
  1699. @subsection Class Definition Protocol
  1700. Here is a summary diagram of the syntax, procedures and generic
  1701. functions that may be involved in class definition.
  1702. @noindent
  1703. @code{define-class} (syntax)
  1704. @itemize @bullet
  1705. @item
  1706. @code{class} (syntax)
  1707. @itemize @bullet
  1708. @item
  1709. @code{make-class} (procedure)
  1710. @itemize @bullet
  1711. @item
  1712. @code{ensure-metaclass} (procedure)
  1713. @item
  1714. @code{make @var{metaclass} @dots{}} (generic)
  1715. @itemize @bullet
  1716. @item
  1717. @code{allocate-instance} (generic)
  1718. @item
  1719. @code{initialize} (generic)
  1720. @itemize @bullet
  1721. @item
  1722. @code{compute-cpl} (generic)
  1723. @itemize @bullet
  1724. @item
  1725. @code{compute-std-cpl} (procedure)
  1726. @end itemize
  1727. @item
  1728. @code{compute-slots} (generic)
  1729. @item
  1730. @code{compute-get-n-set} (generic)
  1731. @item
  1732. @code{compute-getter-method} (generic)
  1733. @item
  1734. @code{compute-setter-method} (generic)
  1735. @end itemize
  1736. @end itemize
  1737. @end itemize
  1738. @end itemize
  1739. @item
  1740. @code{class-redefinition} (generic)
  1741. @itemize @bullet
  1742. @item
  1743. @code{remove-class-accessors} (generic)
  1744. @item
  1745. @code{update-direct-method!} (generic)
  1746. @item
  1747. @code{update-direct-subclass!} (generic)
  1748. @end itemize
  1749. @end itemize
  1750. Wherever a step above is marked as ``generic'', it can be customized,
  1751. and the detail shown below it is only ``correct'' insofar as it
  1752. describes what the default method of that generic function does. For
  1753. example, if you write an @code{initialize} method, for some metaclass,
  1754. that does not call @code{next-method} and does not call
  1755. @code{compute-cpl}, then @code{compute-cpl} will not be called when a
  1756. class is defined with that metaclass.
  1757. A @code{(define-class ...)} form (@pxref{Class Definition}) expands to
  1758. an expression which
  1759. @itemize @bullet
  1760. @item
  1761. checks that it is being evaluated only at top level
  1762. @item
  1763. defines any accessors that are implied by the @var{slot-definition}s
  1764. @item
  1765. uses @code{class} to create the new class
  1766. @item
  1767. checks for a previous class definition for @var{name} and, if found,
  1768. handles the redefinition by invoking @code{class-redefinition}
  1769. (@pxref{Redefining a Class}).
  1770. @end itemize
  1771. @deffn syntax class name (super @dots{}) @
  1772. slot-definition @dots{} class-option @dots{}
  1773. Return a newly created class that inherits from @var{super}s, with
  1774. direct slots defined by @var{slot-definition}s and @var{class-option}s.
  1775. For the format of @var{slot-definition}s and @var{class-option}s, see
  1776. @ref{Class Definition,, define-class}.
  1777. @end deffn
  1778. @noindent @code{class} expands to an expression which
  1779. @itemize @bullet
  1780. @item
  1781. processes the class and slot definition options to check that they are
  1782. well-formed, to convert the @code{#:init-form} option to an
  1783. @code{#:init-thunk} option, to supply a default environment parameter
  1784. (the current top-level environment) and to evaluate all the bits that
  1785. need to be evaluated
  1786. @item
  1787. calls @code{make-class} to create the class with the processed and
  1788. evaluated parameters.
  1789. @end itemize
  1790. @deffn procedure make-class supers slots class-option @dots{}
  1791. Return a newly created class that inherits from @var{supers}, with
  1792. direct slots defined by @var{slots} and @var{class-option}s. For the
  1793. format of @var{slots} and @var{class-option}s, see @ref{Class
  1794. Definition,, define-class}, except note that for @code{make-class},
  1795. @var{slots} is a separate list of slot definitions.
  1796. @end deffn
  1797. @noindent @code{make-class}
  1798. @itemize @bullet
  1799. @item
  1800. adds @code{<object>} to the @var{supers} list if @var{supers} is empty
  1801. or if none of the classes in @var{supers} have @code{<object>} in their
  1802. class precedence list
  1803. @item
  1804. defaults the @code{#:environment}, @code{#:name} and @code{#:metaclass}
  1805. options, if they are not specified by @var{options}, to the current
  1806. top-level environment, the unbound value, and @code{(ensure-metaclass
  1807. @var{supers})} respectively
  1808. @item
  1809. checks for duplicate classes in @var{supers} and duplicate slot names in
  1810. @var{slots}, and signals an error if there are any duplicates
  1811. @item
  1812. calls @code{make}, passing the metaclass as the first parameter and all
  1813. other parameters as option keywords with values.
  1814. @end itemize
  1815. @deffn procedure ensure-metaclass supers env
  1816. Return a metaclass suitable for a class that inherits from the list of
  1817. classes in @var{supers}. The returned metaclass is the union by
  1818. inheritance of the metaclasses of the classes in @var{supers}.
  1819. In the simplest case, where all the @var{supers} are straightforward
  1820. classes with metaclass @code{<class>}, the returned metaclass is just
  1821. @code{<class>}.
  1822. For a more complex example, suppose that @var{supers} contained one
  1823. class with metaclass @code{<operator-class>} and one with metaclass
  1824. @code{<foreign-object-class>}. Then the returned metaclass would be a
  1825. class that inherits from both @code{<operator-class>} and
  1826. @code{<foreign-object-class>}.
  1827. If @var{supers} is the empty list, @code{ensure-metaclass} returns the
  1828. default GOOPS metaclass @code{<class>}.
  1829. GOOPS keeps a list of the metaclasses created by
  1830. @code{ensure-metaclass}, so that each required type of metaclass only
  1831. has to be created once.
  1832. The @code{env} parameter is ignored.
  1833. @end deffn
  1834. @deffn generic make metaclass initarg @dots{}
  1835. @var{metaclass} is the metaclass of the class being defined, either
  1836. taken from the @code{#:metaclass} class option or computed by
  1837. @code{ensure-metaclass}. The applied method must create and return the
  1838. fully initialized class metaobject for the new class definition.
  1839. @end deffn
  1840. The @code{(make @var{metaclass} @var{initarg} @dots{})} invocation is a
  1841. particular case of the instance creation protocol covered in the
  1842. previous section. It will create an class metaobject with metaclass
  1843. @var{metaclass}. By default, this metaobject will be initialized by the
  1844. @code{initialize} method that is specialized for instances of type
  1845. @code{<class>}.
  1846. The @code{initialize} method for classes (signature @code{(initialize
  1847. <class> initargs)}) calls the following generic functions.
  1848. @itemize @bullet
  1849. @item
  1850. @code{compute-cpl @var{class}} (generic)
  1851. The applied method should compute and return the class precedence list
  1852. for @var{class} as a list of class metaobjects. When @code{compute-cpl}
  1853. is called, the following @var{class} metaobject slots have all been
  1854. initialized: @code{name}, @code{direct-supers}, @code{direct-slots},
  1855. @code{direct-subclasses} (empty), @code{direct-methods}. The value
  1856. returned by @code{compute-cpl} will be stored in the @code{cpl} slot.
  1857. @item
  1858. @code{compute-slots @var{class}} (generic)
  1859. The applied method should compute and return the slots (union of direct
  1860. and inherited) for @var{class} as a list of slot definitions. When
  1861. @code{compute-slots} is called, all the @var{class} metaobject slots
  1862. mentioned for @code{compute-cpl} have been initialized, plus the
  1863. following: @code{cpl}, @code{redefined} (@code{#f}), @code{environment}.
  1864. The value returned by @code{compute-slots} will be stored in the
  1865. @code{slots} slot.
  1866. @item
  1867. @code{compute-get-n-set @var{class} @var{slot-def}} (generic)
  1868. @code{initialize} calls @code{compute-get-n-set} for each slot computed
  1869. by @code{compute-slots}. The applied method should compute and return a
  1870. pair of closures that, respectively, get and set the value of the specified
  1871. slot. The get closure should have arity 1 and expect a single argument
  1872. that is the instance whose slot value is to be retrieved. The set closure
  1873. should have arity 2 and expect two arguments, where the first argument is
  1874. the instance whose slot value is to be set and the second argument is the
  1875. new value for that slot. The closures should be returned in a two element
  1876. list: @code{(list @var{get} @var{set})}.
  1877. The closures returned by @code{compute-get-n-set} are stored as part of
  1878. the value of the @var{class} metaobject's @code{getters-n-setters} slot.
  1879. Specifically, the value of this slot is a list with the same number of
  1880. elements as there are slots in the class, and each element looks either like
  1881. @example
  1882. @code{(@var{slot-name-symbol} @var{init-function} . @var{index})}
  1883. @end example
  1884. or like
  1885. @example
  1886. @code{(@var{slot-name-symbol} @var{init-function} @var{get} @var{set})}
  1887. @end example
  1888. Where the get and set closures are replaced by @var{index}, the slot is
  1889. an instance slot and @var{index} is the slot's index in the underlying
  1890. structure: GOOPS knows how to get and set the value of such slots and so
  1891. does not need specially constructed get and set closures. Otherwise,
  1892. @var{get} and @var{set} are the closures returned by @code{compute-get-n-set}.
  1893. The structure of the @code{getters-n-setters} slot value is important when
  1894. understanding the next customizable generic functions that @code{initialize}
  1895. calls@dots{}
  1896. @item
  1897. @code{compute-getter-method @var{class} @var{gns}} (generic)
  1898. @code{initialize} calls @code{compute-getter-method} for each of the
  1899. class's slots (as determined by @code{compute-slots}) that includes a
  1900. @code{#:getter} or @code{#:accessor} slot option. @var{gns} is the
  1901. element of the @var{class} metaobject's @code{getters-n-setters} slot
  1902. that specifies how the slot in question is referenced and set, as
  1903. described above under @code{compute-get-n-set}. The applied method
  1904. should create and return a method that is specialized for instances of
  1905. type @var{class} and uses the get closure to retrieve the slot's value.
  1906. @code{initialize} uses @code{add-method!} to add the returned method to
  1907. the generic function named by the slot definition's @code{#:getter} or
  1908. @code{#:accessor} option.
  1909. @item
  1910. @code{compute-setter-method @var{class} @var{gns}} (generic)
  1911. @code{compute-setter-method} is invoked with the same arguments as
  1912. @code{compute-getter-method}, for each of the class's slots that includes
  1913. a @code{#:setter} or @code{#:accessor} slot option. The applied method
  1914. should create and return a method that is specialized for instances of
  1915. type @var{class} and uses the set closure to set the slot's value.
  1916. @code{initialize} then uses @code{add-method!} to add the returned method
  1917. to the generic function named by the slot definition's @code{#:setter}
  1918. or @code{#:accessor} option.
  1919. @end itemize
  1920. @node Customizing Class Definition
  1921. @subsection Customizing Class Definition
  1922. If the metaclass of the new class is something more specialized than the
  1923. default @code{<class>}, then the type of @var{class} in the calls above
  1924. is more specialized than @code{<class>}, and hence it becomes possible
  1925. to define generic function methods, specialized for the new class's
  1926. metaclass, that can modify or override the default behaviour of
  1927. @code{initialize}, @code{compute-cpl} or @code{compute-get-n-set}.
  1928. @code{compute-cpl} computes the class precedence list (``CPL'') for the
  1929. new class (@pxref{Class Precedence List}), and returns it as a list of
  1930. class objects. The CPL is important because it defines a superclass
  1931. ordering that is used, when a generic function is invoked upon an
  1932. instance of the class, to decide which of the available generic function
  1933. methods is the most specific. Hence @code{compute-cpl} could be
  1934. customized in order to modify the CPL ordering algorithm for all classes
  1935. with a special metaclass.
  1936. The default CPL algorithm is encapsulated by the @code{compute-std-cpl}
  1937. procedure, which is called by the default @code{compute-cpl} method.
  1938. @deffn procedure compute-std-cpl class
  1939. Compute and return the class precedence list for @var{class} according
  1940. to the algorithm described in @ref{Class Precedence List}.
  1941. @end deffn
  1942. @code{compute-slots} computes and returns a list of all slot definitions
  1943. for the new class. By default, this list includes the direct slot
  1944. definitions from the @code{define-class} form, plus the slot definitions
  1945. that are inherited from the new class's superclasses. The default
  1946. @code{compute-slots} method uses the CPL computed by @code{compute-cpl}
  1947. to calculate this union of slot definitions, with the rule that slots
  1948. inherited from superclasses are shadowed by direct slots with the same
  1949. name. One possible reason for customizing @code{compute-slots} would be
  1950. to implement an alternative resolution strategy for slot name conflicts.
  1951. @code{compute-get-n-set} computes the low-level closures that will be
  1952. used to get and set the value of a particular slot, and returns them in
  1953. a list with two elements.
  1954. The closures returned depend on how storage for that slot is allocated.
  1955. The standard @code{compute-get-n-set} method, specialized for classes of
  1956. type @code{<class>}, handles the standard GOOPS values for the
  1957. @code{#:allocation} slot option (@pxref{Slot Options,, allocation}). By
  1958. defining a new @code{compute-get-n-set} method for a more specialized
  1959. metaclass, it is possible to support new types of slot allocation.
  1960. Suppose you wanted to create a large number of instances of some class
  1961. with a slot that should be shared between some but not all instances of
  1962. that class - say every 10 instances should share the same slot storage.
  1963. The following example shows how to implement and use a new type of slot
  1964. allocation to do this.
  1965. @example
  1966. (define-class <batched-allocation-metaclass> (<class>))
  1967. (let ((batch-allocation-count 0)
  1968. (batch-get-n-set #f))
  1969. (define-method (compute-get-n-set
  1970. (class <batched-allocation-metaclass>) s)
  1971. (case (slot-definition-allocation s)
  1972. ((#:batched)
  1973. ;; If we've already used the same slot storage for 10 instances,
  1974. ;; reset variables.
  1975. (if (= batch-allocation-count 10)
  1976. (begin
  1977. (set! batch-allocation-count 0)
  1978. (set! batch-get-n-set #f)))
  1979. ;; If we don't have a current pair of get and set closures,
  1980. ;; create one. make-closure-variable returns a pair of closures
  1981. ;; around a single Scheme variable - see goops.scm for details.
  1982. (or batch-get-n-set
  1983. (set! batch-get-n-set (make-closure-variable)))
  1984. ;; Increment the batch allocation count.
  1985. (set! batch-allocation-count (+ batch-allocation-count 1))
  1986. batch-get-n-set)
  1987. ;; Call next-method to handle standard allocation types.
  1988. (else (next-method)))))
  1989. (define-class <class-using-batched-slot> ()
  1990. ...
  1991. (c #:allocation #:batched)
  1992. ...
  1993. #:metaclass <batched-allocation-metaclass>)
  1994. @end example
  1995. The usage of @code{compute-getter-method} and @code{compute-setter-method}
  1996. is described in @ref{Class Definition Protocol}.
  1997. @code{compute-cpl} and @code{compute-get-n-set} are called by the
  1998. standard @code{initialize} method for classes whose metaclass is
  1999. @code{<class>}. But @code{initialize} itself can also be modified, by
  2000. defining an @code{initialize} method specialized to the new class's
  2001. metaclass. Such a method could complete override the standard
  2002. behaviour, by not calling @code{(next-method)} at all, but more
  2003. typically it would perform additional class initialization steps before
  2004. and/or after calling @code{(next-method)} for the standard behaviour.
  2005. @node Method Definition
  2006. @subsection Method Definition
  2007. @code{define-method} (syntax)
  2008. @itemize @bullet
  2009. @item
  2010. @code{add-method! @var{target} @var{method}} (generic)
  2011. @end itemize
  2012. @noindent
  2013. @code{define-method} invokes the @code{add-method!} generic function to
  2014. handle adding the new method to a variety of possible targets. GOOPS
  2015. includes methods to handle @var{target} as
  2016. @itemize @bullet
  2017. @item
  2018. a generic function (the most common case)
  2019. @item
  2020. a procedure
  2021. @item
  2022. a primitive generic (@pxref{Extending Primitives})
  2023. @end itemize
  2024. By defining further methods for @code{add-method!}, you can
  2025. theoretically handle adding methods to further types of target.
  2026. @node Method Definition Internals
  2027. @subsection Method Definition Internals
  2028. @code{define-method}:
  2029. @itemize @bullet
  2030. @item
  2031. checks the form of the first parameter, and applies the following steps
  2032. to the accessor's setter if it has the @code{(setter @dots{})} form
  2033. @item
  2034. interpolates a call to @code{define-generic} or @code{define-accessor}
  2035. if a generic function is not already defined with the supplied name
  2036. @item
  2037. calls @code{method} with the @var{parameter}s and @var{body}, to make a
  2038. new method instance
  2039. @item
  2040. calls @code{add-method!} to add this method to the relevant generic
  2041. function.
  2042. @end itemize
  2043. @deffn syntax method (parameter @dots{}) body @dots{}
  2044. Make a method whose specializers are defined by the classes in
  2045. @var{parameter}s and whose procedure definition is constructed from the
  2046. @var{parameter} symbols and @var{body} forms.
  2047. The @var{parameter} and @var{body} parameters should be as for
  2048. @code{define-method} (@pxref{Methods and Generic Functions,,
  2049. define-method}).
  2050. @end deffn
  2051. @noindent
  2052. @code{method}:
  2053. @itemize @bullet
  2054. @item
  2055. extracts formals and specializing classes from the @var{parameter}s,
  2056. defaulting the class for unspecialized parameters to @code{<top>}
  2057. @item
  2058. creates a closure using the formals and the @var{body} forms
  2059. @item
  2060. calls @code{make} with metaclass @code{<method>} and the specializers
  2061. and closure using the @code{#:specializers} and @code{#:procedure}
  2062. keywords.
  2063. @end itemize
  2064. @deffn procedure make-method specializers procedure
  2065. Make a method using @var{specializers} and @var{procedure}.
  2066. @var{specializers} should be a list of classes that specifies the
  2067. parameter combinations to which this method will be applicable.
  2068. @var{procedure} should be the closure that will applied to the generic
  2069. function parameters when this method is invoked.
  2070. @end deffn
  2071. @noindent
  2072. @code{make-method} is a simple wrapper around @code{make} with metaclass
  2073. @code{<method>}.
  2074. @deffn generic add-method! target method
  2075. Generic function for adding method @var{method} to @var{target}.
  2076. @end deffn
  2077. @deffn method add-method! (generic <generic>) (method <method>)
  2078. Add method @var{method} to the generic function @var{generic}.
  2079. @end deffn
  2080. @deffn method add-method! (proc <procedure>) (method <method>)
  2081. If @var{proc} is a procedure with generic capability (@pxref{Extending
  2082. Primitives,, generic-capability?}), upgrade it to a primitive generic
  2083. and add @var{method} to its generic function definition.
  2084. @end deffn
  2085. @deffn method add-method! (pg <primitive-generic>) (method <method>)
  2086. Add method @var{method} to the generic function definition of @var{pg}.
  2087. Implementation: @code{(add-method! (primitive-generic-generic pg) method)}.
  2088. @end deffn
  2089. @deffn method add-method! (whatever <top>) (method <method>)
  2090. Raise an error indicating that @var{whatever} is not a valid generic
  2091. function.
  2092. @end deffn
  2093. @node Generic Function Internals
  2094. @subsection Generic Function Internals
  2095. @code{define-generic} calls @code{ensure-generic} to upgrade a
  2096. pre-existing procedure value, or @code{make} with metaclass
  2097. @code{<generic>} to create a new generic function.
  2098. @code{define-accessor} calls @code{ensure-accessor} to upgrade a
  2099. pre-existing procedure value, or @code{make-accessor} to create a new
  2100. accessor.
  2101. @deffn procedure ensure-generic old-definition [name]
  2102. Return a generic function with name @var{name}, if possible by using or
  2103. upgrading @var{old-definition}. If unspecified, @var{name} defaults to
  2104. @code{#f}.
  2105. If @var{old-definition} is already a generic function, it is returned
  2106. unchanged.
  2107. If @var{old-definition} is a Scheme procedure or procedure-with-setter,
  2108. @code{ensure-generic} returns a new generic function that uses
  2109. @var{old-definition} for its default procedure and setter.
  2110. Otherwise @code{ensure-generic} returns a new generic function with no
  2111. defaults and no methods.
  2112. @end deffn
  2113. @deffn procedure make-generic [name]
  2114. Return a new generic function with name @code{(car @var{name})}. If
  2115. unspecified, @var{name} defaults to @code{#f}.
  2116. @end deffn
  2117. @code{ensure-generic} calls @code{make} with metaclasses
  2118. @code{<generic>} and @code{<generic-with-setter>}, depending on the
  2119. previous value of the variable that it is trying to upgrade.
  2120. @code{make-generic} is a simple wrapper for @code{make} with metaclass
  2121. @code{<generic>}.
  2122. @deffn procedure ensure-accessor proc [name]
  2123. Return an accessor with name @var{name}, if possible by using or
  2124. upgrading @var{proc}. If unspecified, @var{name} defaults to @code{#f}.
  2125. If @var{proc} is already an accessor, it is returned unchanged.
  2126. If @var{proc} is a Scheme procedure, procedure-with-setter or generic
  2127. function, @code{ensure-accessor} returns an accessor that reuses the
  2128. reusable elements of @var{proc}.
  2129. Otherwise @code{ensure-accessor} returns a new accessor with no defaults
  2130. and no methods.
  2131. @end deffn
  2132. @deffn procedure make-accessor [name]
  2133. Return a new accessor with name @code{(car @var{name})}. If
  2134. unspecified, @var{name} defaults to @code{#f}.
  2135. @end deffn
  2136. @code{ensure-accessor} calls @code{make} with
  2137. metaclass @code{<generic-with-setter>}, as well as calls to
  2138. @code{ensure-generic}, @code{make-accessor} and (tail recursively)
  2139. @code{ensure-accessor}.
  2140. @code{make-accessor} calls @code{make} twice, first
  2141. with metaclass @code{<generic>} to create a generic function for the
  2142. setter, then with metaclass @code{<generic-with-setter>} to create the
  2143. accessor, passing the setter generic function as the value of the
  2144. @code{#:setter} keyword.
  2145. @node Generic Function Invocation
  2146. @subsection Generic Function Invocation
  2147. There is a detailed and customizable protocol involved in the process of
  2148. invoking a generic function --- i.e., in the process of deciding which
  2149. of the generic function's methods are applicable to the current
  2150. arguments, and which one of those to apply. Here is a summary diagram
  2151. of the generic functions involved.
  2152. @noindent
  2153. @code{apply-generic} (generic)
  2154. @itemize @bullet
  2155. @item
  2156. @code{no-method} (generic)
  2157. @item
  2158. @code{compute-applicable-methods} (generic)
  2159. @item
  2160. @code{sort-applicable-methods} (generic)
  2161. @itemize @bullet
  2162. @item
  2163. @code{method-more-specific?} (generic)
  2164. @end itemize
  2165. @item
  2166. @code{apply-methods} (generic)
  2167. @itemize @bullet
  2168. @item
  2169. @code{apply-method} (generic)
  2170. @item
  2171. @code{no-next-method} (generic)
  2172. @end itemize
  2173. @item
  2174. @code{no-applicable-method}
  2175. @end itemize
  2176. We do not yet have full documentation for these. Please refer to the
  2177. code (@file{oop/goops.scm}) for details.
  2178. @node Redefining a Class
  2179. @section Redefining a Class
  2180. Suppose that a class @code{<my-class>} is defined using @code{define-class}
  2181. (@pxref{Class Definition,, define-class}), with slots that have
  2182. accessor functions, and that an application has created several instances
  2183. of @code{<my-class>} using @code{make} (@pxref{Instance Creation,,
  2184. make}). What then happens if @code{<my-class>} is redefined by calling
  2185. @code{define-class} again?
  2186. @menu
  2187. * Redefinable Classes::
  2188. * Default Class Redefinition Behaviour::
  2189. * Customizing Class Redefinition::
  2190. @end menu
  2191. @node Redefinable Classes
  2192. @subsection Redefinable Classes
  2193. The ability for a class to be redefined is a choice for a class author
  2194. to make. By default, classes in GOOPS are @emph{not} redefinable. A
  2195. redefinable class is an instance of @code{<redefinable-class>}; that is
  2196. to say, a class with @code{<redefinable-class>} as its metaclass.
  2197. Accordingly, to define a redefinable class, add @code{#:metaclass
  2198. <redefinable-class>} to its class definition:
  2199. @example
  2200. (define-class <foo> ()
  2201. #:metaclass <redefinable-class>)
  2202. @end example
  2203. Note that any subclass of @code{<foo>} is also redefinable, without the
  2204. need to explicitly pass the @code{#:metaclass} argument, so you only
  2205. need to specify @code{#:metaclass} for the roots of your application's
  2206. class hierarchy.
  2207. @example
  2208. (define-class <bar> (<foo>))
  2209. (class-of <bar>) @result{} <redefinable-class>
  2210. @end example
  2211. Note that prior to Guile 3.0, all GOOPS classes were redefinable in
  2212. theory. In practice, attempting to, for example, redefine
  2213. @code{<class>} itself would almost certainly not do what you want.
  2214. Still, redefinition is an interesting capability when building
  2215. long-lived resilient systems, so GOOPS does offer this facility.
  2216. @node Default Class Redefinition Behaviour
  2217. @subsection Default Class Redefinition Behaviour
  2218. When a class is defined using @code{define-class} and the class name was
  2219. previously defined, by default the new binding just replaces the old
  2220. binding. This is the normal behavior for @code{define}. However if
  2221. both the old and new bindings are redefinable classes (instances of
  2222. @code{<redefinable-class>}), then the class will be updated in place,
  2223. and its instances lazily migrated over.
  2224. The way that the class is updated and the way that the instances migrate
  2225. over are of course part of the meta-object protocol. However the
  2226. default behavior usually suffices, and it goes as follows.
  2227. @itemize @bullet
  2228. @item
  2229. All existing direct instances of @code{<my-class>} are converted to be
  2230. instances of the new class. This is achieved by preserving the values
  2231. of slots that exist in both the old and new definitions, and
  2232. initializing the values of new slots in the usual way (@pxref{Instance
  2233. Creation,, make}).
  2234. @item
  2235. All existing subclasses of @code{<my-class>} are redefined, as though
  2236. the @code{define-class} expressions that defined them were re-evaluated
  2237. following the redefinition of @code{<my-class>}, and the class
  2238. redefinition process described here is applied recursively to the
  2239. redefined subclasses.
  2240. @item
  2241. Once all of its instances and subclasses have been updated, the class
  2242. metaobject previously bound to the variable @code{<my-class>} is no
  2243. longer needed and so can be allowed to be garbage collected.
  2244. @end itemize
  2245. To keep things tidy, GOOPS also needs to do a little housekeeping on
  2246. methods that are associated with the redefined class.
  2247. @itemize @bullet
  2248. @item
  2249. Slot accessor methods for slots in the old definition should be removed
  2250. from their generic functions. They will be replaced by accessor methods
  2251. for the slots of the new class definition.
  2252. @item
  2253. Any generic function method that uses the old @code{<my-class>} metaobject
  2254. as one of its formal parameter specializers must be updated to refer to
  2255. the new @code{<my-class>} metaobject. (Whenever a new generic function
  2256. method is defined, @code{define-method} adds the method to a list stored
  2257. in the class metaobject for each class used as a formal parameter
  2258. specializer, so it is easy to identify all the methods that must be
  2259. updated when a class is redefined.)
  2260. @end itemize
  2261. If this class redefinition strategy strikes you as rather counter-intuitive,
  2262. bear in mind that it is derived from similar behaviour in other object
  2263. systems such as CLOS, and that experience in those systems has shown it to be
  2264. very useful in practice.
  2265. Also bear in mind that, like most of GOOPS' default behaviour, it can
  2266. be customized@dots{}
  2267. @node Customizing Class Redefinition
  2268. @subsection Customizing Class Redefinition
  2269. When @code{define-class} notices that a class is being redefined, it
  2270. constructs the new class metaobject as usual, then invokes the
  2271. @code{class-redefinition} generic function with the old and new classes
  2272. as arguments. Therefore, if the old or new classes have metaclasses
  2273. other than the default @code{<redefinable-class>}, class redefinition
  2274. behaviour can be customized by defining a @code{class-redefinition}
  2275. method that is specialized for the relevant metaclasses.
  2276. @deffn generic class-redefinition
  2277. Handle the class redefinition from @var{old} to @var{new}, and return
  2278. the new class metaobject that should be bound to the variable specified
  2279. by @code{define-class}'s first argument.
  2280. @end deffn
  2281. @deffn method class-redefinition (old <top>) (new <class>)
  2282. Not all classes are redefinable, and not all previous bindings are
  2283. classes. @xref{Redefinable Classes}. This default method just returns
  2284. @var{new}.
  2285. @end deffn
  2286. @deffn method class-redefinition (old <redefinable-class>) (new <redefinable-class>)
  2287. This method implements GOOPS' default class redefinition behaviour, as
  2288. described in @ref{Default Class Redefinition Behaviour}. Returns the
  2289. metaobject for the new class definition.
  2290. @end deffn
  2291. The @code{class-redefinition} method for classes with metaclass
  2292. @code{<redefinable-class>} calls the following generic functions, which
  2293. could of course be individually customized.
  2294. @deffn generic remove-class-accessors! old
  2295. The default @code{remove-class-accessors!} method removes the accessor
  2296. methods of the old class from all classes which they specialize.
  2297. @end deffn
  2298. @deffn generic update-direct-method! method old new
  2299. The default @code{update-direct-method!} method substitutes the new
  2300. class for the old in all methods specialized to the old class.
  2301. @end deffn
  2302. @deffn generic update-direct-subclass! subclass old new
  2303. The default @code{update-direct-subclass!} method invokes
  2304. @code{class-redefinition} recursively to handle the redefinition of
  2305. subclasses.
  2306. @end deffn
  2307. An alternative class redefinition strategy could be to leave all
  2308. existing instances as instances of the old class, but accepting that the
  2309. old class is now ``nameless'', since its name has been taken over by the
  2310. new definition. In this strategy, any existing subclasses could also
  2311. be left as they are, on the understanding that they inherit from a nameless
  2312. superclass.
  2313. This strategy is easily implemented in GOOPS, by defining a new metaclass,
  2314. that will be used as the metaclass for all classes to which the strategy
  2315. should apply, and then defining a @code{class-redefinition} method that
  2316. is specialized for this metaclass:
  2317. @example
  2318. (define-class <can-be-nameless> (<redefinable-class>))
  2319. (define-method (class-redefinition (old <can-be-nameless>)
  2320. (new <class>))
  2321. new)
  2322. @end example
  2323. When customization can be as easy as this, aren't you glad that GOOPS
  2324. implements the far more difficult strategy as its default!
  2325. @node Changing the Class of an Instance
  2326. @section Changing the Class of an Instance
  2327. When a redefinable class is redefined, any existing instance of the
  2328. redefined class will be modified for the new class definition before the
  2329. next time that any of the instance's slots is referenced or set. GOOPS
  2330. modifies each instance by calling the generic function
  2331. @code{change-class}.
  2332. More generally, you can change the class of an existing instance at any
  2333. time by invoking the generic function @code{change-class} with two
  2334. arguments: the instance and the new class.
  2335. The default method for @code{change-class} decides how to implement the
  2336. change of class by looking at the slot definitions for the instance's
  2337. existing class and for the new class. If the new class has slots with
  2338. the same name as slots in the existing class, the values for those slots
  2339. are preserved. Slots that are present only in the existing class are
  2340. discarded. Slots that are present only in the new class are initialized
  2341. using the corresponding slot definition's init function (@pxref{Classes,,
  2342. slot-init-function}).
  2343. @deffn generic change-class instance new-class
  2344. @end deffn
  2345. @deffn {method} change-class (obj <object>) (new <redefinable-class>)
  2346. Modify instance @var{obj} to make it an instance of class @var{new}.
  2347. @var{obj} itself must already be an instance of a redefinable class.
  2348. The value of each of @var{obj}'s slots is preserved only if a similarly named
  2349. slot exists in @var{new}; any other slot values are discarded.
  2350. The slots in @var{new} that do not correspond to any of @var{obj}'s
  2351. pre-existing slots are initialized according to @var{new}'s slot definitions'
  2352. init functions.
  2353. @end deffn
  2354. The default @code{change-class} method also invokes another generic
  2355. function, @code{update-instance-for-different-class}, as the last thing
  2356. that it does before returning. The applied
  2357. @code{update-instance-for-different-class} method can make any further
  2358. adjustments to @var{new-instance} that are required to complete or
  2359. modify the change of class. The return value from the applied method is
  2360. ignored.
  2361. @deffn generic update-instance-for-different-class old-instance new-instance
  2362. A generic function that can be customized to put finishing touches to an
  2363. instance whose class has just been changed. The default
  2364. @code{update-instance-for-different-class} method does nothing.
  2365. @end deffn
  2366. Customized change of class behaviour can be implemented by defining
  2367. @code{change-class} methods that are specialized either by the class
  2368. of the instances to be modified or by the metaclass of the new class.