goops.texi 102 KB

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