eieio.texi 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. \input texinfo
  2. @setfilename ../../info/eieio.info
  3. @set TITLE Enhanced Implementation of Emacs Interpreted Objects
  4. @set AUTHOR Eric M. Ludlam
  5. @settitle @value{TITLE}
  6. @include docstyle.texi
  7. @c *************************************************************************
  8. @c @ Header
  9. @c *************************************************************************
  10. @copying
  11. This manual documents EIEIO, an object framework for Emacs Lisp.
  12. Copyright @copyright{} 2007--2016 Free Software Foundation, Inc.
  13. @quotation
  14. Permission is granted to copy, distribute and/or modify this document
  15. under the terms of the GNU Free Documentation License, Version 1.3 or
  16. any later version published by the Free Software Foundation; with no
  17. Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
  18. and with the Back-Cover Texts as in (a) below. A copy of the license
  19. is included in the section entitled ``GNU Free Documentation License.''
  20. (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
  21. modify this GNU manual.''
  22. @end quotation
  23. @end copying
  24. @dircategory Emacs misc features
  25. @direntry
  26. * EIEIO: (eieio). An objects system for Emacs Lisp.
  27. @end direntry
  28. @titlepage
  29. @center @titlefont{@value{TITLE}}
  30. @sp 4
  31. @center by @value{AUTHOR}
  32. @page
  33. @vskip 0pt plus 1filll
  34. @insertcopying
  35. @end titlepage
  36. @macro eieio{}
  37. @i{EIEIO}
  38. @end macro
  39. @node Top
  40. @top EIEIO
  41. @eieio{} (``Enhanced Implementation of Emacs Interpreted Objects'')
  42. provides an Object Oriented layer for Emacs Lisp, following the basic
  43. concepts of the Common Lisp Object System (CLOS). It provides a
  44. framework for writing object-oriented applications in Emacs.
  45. @ifnottex
  46. @insertcopying
  47. @end ifnottex
  48. @menu
  49. * Quick Start:: Quick start for EIEIO.
  50. * Introduction:: Why use @eieio{}? Basic overview, samples list.
  51. * Building Classes:: How to write new class structures.
  52. * Making New Objects:: How to construct new objects.
  53. * Accessing Slots:: How to access a slot.
  54. * Writing Methods:: How to write a method.
  55. * Method Invocation:: How methods are invoked.
  56. * Predicates:: Class-p, Object-p, etc-p.
  57. * Association Lists:: List of objects as association lists.
  58. * Customizing:: Customizing objects.
  59. * Introspection:: Looking inside a class.
  60. * Base Classes:: Additional classes you can inherit from.
  61. * Browsing:: Browsing your class lists.
  62. * Class Values:: Displaying information about a class or object.
  63. * Default Superclass:: The root superclasses.
  64. * Signals:: When you make errors.
  65. * Naming Conventions:: Name your objects in an Emacs friendly way.
  66. * CLOS compatibility:: What are the differences?
  67. * Wish List:: Things about EIEIO that could be improved.
  68. * GNU Free Documentation License:: The license for this documentation.
  69. * Function Index::
  70. @end menu
  71. @node Quick Start
  72. @chapter Quick Start
  73. @eieio{} provides an Object Oriented layer for Emacs Lisp. You can
  74. use @eieio{} to create classes, methods for those classes, and
  75. instances of classes.
  76. Here is a simple example of a class named @code{record}, containing
  77. three slots named @code{name}, @code{birthday}, and @code{phone}:
  78. @example
  79. (defclass record () ; No superclasses
  80. ((name :initarg :name
  81. :initform ""
  82. :type string
  83. :custom string
  84. :documentation "The name of a person.")
  85. (birthday :initarg :birthday
  86. :initform "Jan 1, 1970"
  87. :custom string
  88. :type string
  89. :documentation "The person's birthday.")
  90. (phone :initarg :phone
  91. :initform ""
  92. :documentation "Phone number."))
  93. "A single record for tracking people I know.")
  94. @end example
  95. Each class can have methods, which are defined like this:
  96. @example
  97. (defmethod call-record ((rec record) &optional scriptname)
  98. "Dial the phone for the record REC.
  99. Execute the program SCRIPTNAME to dial the phone."
  100. (message "Dialing the phone for %s" (oref rec name))
  101. (shell-command (concat (or scriptname "dialphone.sh")
  102. " "
  103. (oref rec phone))))
  104. @end example
  105. @noindent
  106. In this example, the first argument to @code{call-record} is a list,
  107. of the form (@var{varname} @var{classname}). @var{varname} is the
  108. name of the variable used for the first argument; @var{classname} is
  109. the name of the class that is expected as the first argument for this
  110. method.
  111. @eieio{} dispatches methods based on the type of the first argument.
  112. You can have multiple methods with the same name for different classes
  113. of object. When the @code{call-record} method is called, the first
  114. argument is examined to determine the class of that argument, and the
  115. method matching the input type is then executed.
  116. Once the behavior of a class is defined, you can create a new
  117. object of type @code{record}. Objects are created by calling the
  118. constructor. The constructor is a function with the same name as your
  119. class which returns a new instance of that class. Here is an example:
  120. @example
  121. (setq rec (record :name "Eric" :birthday "June" :phone "555-5555"))
  122. @end example
  123. @noindent
  124. For backward compatibility reasons, the first argument can be a string (a name
  125. given to this instance). Each instance used to be given a name, so different
  126. instances could be easily distinguished when debugging.
  127. It can be a bit repetitive to also have a :name slot. To avoid doing
  128. this, it is sometimes handy to use the base class @code{eieio-named}.
  129. @xref{eieio-named}.
  130. Calling methods on an object is a lot like calling any function. The
  131. first argument should be an object of a class which has had this
  132. method defined for it. In this example it would look like this:
  133. @example
  134. (call-record rec)
  135. @end example
  136. @noindent
  137. or
  138. @example
  139. (call-record rec "my-call-script")
  140. @end example
  141. In these examples, @eieio{} automatically examines the class of
  142. @code{rec}, and ensures that the method defined above is called. If
  143. @code{rec} is some other class lacking a @code{call-record} method, or
  144. some other data type, Emacs signals a @code{no-method-definition}
  145. error. @ref{Signals}.
  146. @node Introduction
  147. @chapter Introduction
  148. First off, please note that this manual cannot serve as a complete
  149. introduction to object oriented programming and generic functions in
  150. LISP@. Although EIEIO is not a complete implementation of the Common
  151. Lisp Object System (CLOS) and also differs from it in several aspects,
  152. it follows the same basic concepts. Therefore, it is highly
  153. recommended to learn those from a textbook or tutorial first,
  154. especially if you only know OOP from languages like C++ or Java. If
  155. on the other hand you are already familiar with CLOS, you should be
  156. aware that @eieio{} does not implement the full CLOS specification and
  157. also differs in some other aspects which are mentioned below (also
  158. @pxref{CLOS compatibility}).
  159. @eieio{} supports the following features:
  160. @enumerate
  161. @item
  162. A structured framework for the creation of basic classes with attributes
  163. and methods using singular inheritance similar to CLOS.
  164. @item
  165. Type checking, and slot unbinding.
  166. @item
  167. Method definitions similar to CLOS.
  168. @item
  169. Simple and complex class browsers.
  170. @item
  171. Edebug support for methods.
  172. @item
  173. Imenu updates.
  174. @item
  175. Byte compilation support of methods.
  176. @item
  177. Help system extensions for classes and methods.
  178. @item
  179. Several base classes for interesting tasks.
  180. @item
  181. Simple test suite.
  182. @item
  183. Public and private classifications for slots (extensions to CLOS)
  184. @item
  185. Customization support in a class (extension to CLOS)
  186. @end enumerate
  187. Due to restrictions in the Emacs Lisp language, CLOS cannot be
  188. completely supported, and a few functions have been added in place of
  189. setf. Here are some important CLOS features that @eieio{} presently
  190. lacks:
  191. @table @asis
  192. @item Method dispatch
  193. EIEO does not support method dispatch for built-in types and multiple
  194. arguments types. In other words, method dispatch only looks at the
  195. first argument, and this one must be an @eieio{} type.
  196. @item Support for metaclasses
  197. There is just one default metaclass, @code{eieio-default-superclass},
  198. and you cannot define your own. The @code{:metaclass} tag in
  199. @code{defclass} is ignored. Also, functions like @code{find-class}, which
  200. should return instances of the metaclass, behave differently in
  201. @eieio{} in that they return symbols or plain structures instead.
  202. @item EQL specialization
  203. EIEIO does not support it.
  204. @item @code{:around} method tag
  205. This CLOS method tag is non-functional.
  206. @item :default-initargs in @code{defclass}
  207. Each slot can have an @code{:initform} tag, so this is not really necessary.
  208. @item Mock object initializers
  209. Each class contains a mock object used for fast initialization of
  210. instantiated objects. Using functions with side effects on object slot
  211. values can potentially cause modifications in the mock object. @eieio{}
  212. should use a deep copy but currently does not.
  213. @end table
  214. @node Building Classes
  215. @chapter Building Classes
  216. A @dfn{class} is a definition for organizing data and methods
  217. together. An @eieio{} class has structures similar to the classes
  218. found in other object-oriented (OO) languages.
  219. To create a new class, use the @code{defclass} macro:
  220. @defmac defclass class-name superclass-list slot-list &rest options-and-doc
  221. Create a new class named @var{class-name}. The class is represented
  222. by a symbol with the name @var{class-name}. @eieio{} stores the structure of
  223. the class as a symbol property of @var{class-name} (@pxref{Symbol
  224. Components,,,elisp,GNU Emacs Lisp Reference Manual}).
  225. The @var{class-name} symbol's variable documentation string is a
  226. modified version of the doc string found in @var{options-and-doc}.
  227. Each time a method is defined, the symbol's documentation string is
  228. updated to include the methods documentation as well.
  229. The parent classes for @var{class-name} is @var{superclass-list}.
  230. Each element of @var{superclass-list} must be a class. These classes
  231. are the parents of the class being created. Every slot that appears
  232. in each parent class is replicated in the new class.
  233. If two parents share the same slot name, the parent which appears in
  234. the @var{superclass-list} first sets the tags for that slot. If the
  235. new class has a slot with the same name as the parent, the new slot
  236. overrides the parent's slot.
  237. When overriding a slot, some slot attributes cannot be overridden
  238. because they break basic OO rules. You cannot override @code{:type}
  239. or @code{:protection}.
  240. @end defmac
  241. @noindent
  242. Whenever defclass is used to create a new class, a predicate is
  243. created for it, named @code{@var{CLASS-NAME}-p}:
  244. @defun CLASS-NAME-p object
  245. Return non-@code{nil} if and only if @var{OBJECT} is of the class
  246. @var{CLASS-NAME}.
  247. @end defun
  248. @defvar eieio-error-unsupported-class-tags
  249. If non-@code{nil}, @code{defclass} signals an error if a tag in a slot
  250. specifier is unsupported.
  251. This option is here to support programs written with older versions of
  252. @eieio{}, which did not produce such errors.
  253. @end defvar
  254. @menu
  255. * Inheritance:: How to specify parents classes.
  256. * Slot Options:: How to specify features of a slot.
  257. * Class Options:: How to specify features for this class.
  258. @end menu
  259. @node Inheritance
  260. @section Inheritance
  261. @dfn{Inheritance} is a basic feature of an object-oriented language.
  262. In @eieio{}, a defined class specifies the super classes from which it
  263. inherits by using the second argument to @code{defclass}. Here is an
  264. example:
  265. @example
  266. (defclass my-baseclass ()
  267. ((slot-A :initarg :slot-A)
  268. (slot-B :initarg :slot-B))
  269. "My Baseclass.")
  270. @end example
  271. @noindent
  272. To subclass from @code{my-baseclass}, we specify it in the superclass
  273. list:
  274. @example
  275. (defclass my-subclass (my-baseclass)
  276. ((specific-slot-A :initarg specific-slot-A)
  277. )
  278. "My subclass of my-baseclass")
  279. @end example
  280. @indent
  281. Instances of @code{my-subclass} will inherit @code{slot-A} and
  282. @code{slot-B}, in addition to having @code{specific-slot-A} from the
  283. declaration of @code{my-subclass}.
  284. @eieio{} also supports multiple inheritance. Suppose we define a
  285. second baseclass, perhaps an ``interface'' class, like this:
  286. @example
  287. (defclass my-interface ()
  288. ((interface-slot :initarg :interface-slot))
  289. "An interface to special behavior."
  290. :abstract t)
  291. @end example
  292. @noindent
  293. The interface class defines a special @code{interface-slot}, and also
  294. specifies itself as abstract. Abstract classes cannot be
  295. instantiated. It is not required to make interfaces abstract, but it
  296. is a good programming practice.
  297. We can now modify our definition of @code{my-subclass} to use this
  298. interface class, together with our original base class:
  299. @example
  300. (defclass my-subclass (my-baseclass my-interface)
  301. ((specific-slot-A :initarg specific-slot-A)
  302. )
  303. "My subclass of my-baseclass")
  304. @end example
  305. @noindent
  306. With this, @code{my-subclass} also has @code{interface-slot}.
  307. If @code{my-baseclass} and @code{my-interface} had slots with the same
  308. name, then the superclass showing up in the list first defines the
  309. slot attributes.
  310. Inheritance in @eieio{} is more than just combining different slots.
  311. It is also important in method invocation. @ref{Methods}.
  312. If a method is called on an instance of @code{my-subclass}, and that
  313. method only has an implementation on @code{my-baseclass}, or perhaps
  314. @code{my-interface}, then the implementation for the baseclass is
  315. called.
  316. If there is a method implementation for @code{my-subclass}, and
  317. another in @code{my-baseclass}, the implementation for
  318. @code{my-subclass} can call up to the superclass as well.
  319. @node Slot Options
  320. @section Slot Options
  321. The @var{slot-list} argument to @code{defclass} is a list of elements
  322. where each element defines one slot. Each slot is a list of the form
  323. @example
  324. (SLOT-NAME :TAG1 ATTRIB-VALUE1
  325. :TAG2 ATTRIB-VALUE2
  326. :TAGN ATTRIB-VALUEN)
  327. @end example
  328. @noindent
  329. where @var{SLOT-NAME} is a symbol that will be used to refer to the
  330. slot. @var{:TAG} is a symbol that describes a feature to be set
  331. on the slot. @var{ATTRIB-VALUE} is a lisp expression that will be
  332. used for @var{:TAG}.
  333. Valid tags are:
  334. @table @code
  335. @item :initarg
  336. A symbol that can be used in the argument list of the constructor to
  337. specify a value for this slot of the new instance being created.
  338. A good symbol to use for initarg is one that starts with a colon @code{:}.
  339. The slot specified like this:
  340. @example
  341. (myslot :initarg :myslot)
  342. @end example
  343. could then be initialized to the number 1 like this:
  344. @example
  345. (myobject :myslot 1)
  346. @end example
  347. @xref{Making New Objects}.
  348. @item :initform
  349. An expression used as the default value for this slot.
  350. If @code{:initform} is left out, that slot defaults to being unbound.
  351. It is an error to reference an unbound slot, so if you need
  352. slots to always be in a bound state, you should always use an
  353. @code{:initform} specifier.
  354. Use @code{slot-boundp} to test if a slot is unbound
  355. (@pxref{Predicates}). Use @code{slot-makeunbound} to set a slot to
  356. being unbound after giving it a value (@pxref{Accessing Slots}).
  357. The value passed to initform used to be automatically quoted. Thus,
  358. @example
  359. :initform (1 2 3)
  360. @end example
  361. will use the list as a value. This is incompatible with CLOS (which would
  362. signal an error since 1 is not a valid function) and will likely change in the
  363. future, so better quote your initforms if they're just values.
  364. @item :type
  365. An unquoted type specifier used to validate data set into this slot.
  366. @xref{Type Predicates,,,cl,Common Lisp Extensions}.
  367. Here are some examples:
  368. @table @code
  369. @item symbol
  370. A symbol.
  371. @item number
  372. A number type
  373. @item my-class-name
  374. An object of your class type.
  375. @item (or null symbol)
  376. A symbol, or @code{nil}.
  377. @end table
  378. @item :allocation
  379. Either :class or :instance (defaults to :instance) used to
  380. specify how data is stored. Slots stored per instance have unique
  381. values for each object. Slots stored per class have shared values for
  382. each object. If one object changes a :class allocated slot, then all
  383. objects for that class gain the new value.
  384. @item :documentation
  385. Documentation detailing the use of this slot. This documentation is
  386. exposed when the user describes a class, and during customization of an
  387. object.
  388. @item :accessor
  389. Name of a generic function which can be used to fetch the value of this slot.
  390. You can call this function later on your object and retrieve the value
  391. of the slot.
  392. This options is in the CLOS spec, but is not fully compliant in @eieio{}.
  393. @item :writer
  394. Name of a generic function which will write this slot.
  395. This options is in the CLOS spec, but is not fully compliant in @eieio{}.
  396. @item :reader
  397. Name of a generic function which will read this slot.
  398. This options is in the CLOS spec, but is not fully compliant in @eieio{}.
  399. @item :custom
  400. A custom :type specifier used when editing an object of this type.
  401. See documentation for @code{defcustom} for details. This specifier is
  402. equivalent to the :type spec of a @code{defcustom} call.
  403. This options is specific to Emacs, and is not in the CLOS spec.
  404. @item :label
  405. When customizing an object, the value of :label will be used instead
  406. of the slot name. This enables better descriptions of the data than
  407. would usually be afforded.
  408. This options is specific to Emacs, and is not in the CLOS spec.
  409. @item :group
  410. Similar to @code{defcustom}'s :group command, this organizes different
  411. slots in an object into groups. When customizing an object, only the
  412. slots belonging to a specific group need be worked with, simplifying the
  413. size of the display.
  414. This options is specific to Emacs, and is not in the CLOS spec.
  415. @item :printer
  416. This routine takes a symbol which is a function name. The function
  417. should accept one argument. The argument is the value from the slot
  418. to be printed. The function in @code{object-write} will write the
  419. slot value out to a printable form on @code{standard-output}.
  420. The output format MUST be something that could in turn be interpreted
  421. with @code{read} such that the object can be brought back in from the
  422. output stream. Thus, if you wanted to output a symbol, you would need
  423. to quote the symbol. If you wanted to run a function on load, you
  424. can output the code to do the construction of the value.
  425. @item :protection
  426. This is an old option that is not supported any more.
  427. When using a slot referencing function such as @code{slot-value}, and
  428. the value behind @var{slot} is private or protected, then the current
  429. scope of operation must be within a method of the calling object.
  430. This protection is not enforced by the code any more, so it's only useful
  431. as documentation.
  432. Valid values are:
  433. @table @code
  434. @item :public
  435. Access this slot from any scope.
  436. @item :protected
  437. Access this slot only from methods of the same class or a child class.
  438. @item :private
  439. Access this slot only from methods of the same class.
  440. @end table
  441. This options is specific to Emacs, and is not in the CLOS spec.
  442. @end table
  443. @node Class Options
  444. @section Class Options
  445. In the @var{options-and-doc} arguments to @code{defclass}, the
  446. following class options may be specified:
  447. @table @code
  448. @item :documentation
  449. A documentation string for this class.
  450. If an Emacs-style documentation string is also provided, then this
  451. option is ignored. An Emacs-style documentation string is not
  452. prefixed by the @code{:documentation} tag, and appears after the list
  453. of slots, and before the options.
  454. @item :allow-nil-initform
  455. If this option is non-@code{nil}, and the @code{:initform} is @code{nil}, but
  456. the @code{:type} is specifies something such as @code{string} then allow
  457. this to pass. The default is to have this option be off. This is
  458. implemented as an alternative to unbound slots.
  459. This options is specific to Emacs, and is not in the CLOS spec.
  460. @item :abstract
  461. A class which is @code{:abstract} cannot be instantiated, and instead
  462. is used to define an interface which subclasses should implement.
  463. This option is specific to Emacs, and is not in the CLOS spec.
  464. @item :custom-groups
  465. This is a list of groups that can be customized within this class. This
  466. slot is auto-generated when a class is created and need not be
  467. specified. It can be retrieved with the @code{class-option} command,
  468. however, to see what groups are available.
  469. This option is specific to Emacs, and is not in the CLOS spec.
  470. @item :method-invocation-order
  471. This controls the order in which method resolution occurs for
  472. @code{:primary} methods in cases of multiple inheritance. The order
  473. affects which method is called first in a tree, and if
  474. @code{call-next-method} is used, it controls the order in which the
  475. stack of methods are run.
  476. Valid values are:
  477. @table @code
  478. @item :breadth-first
  479. Search for methods in the class hierarchy in breadth first order.
  480. This is the default.
  481. @item :depth-first
  482. Search for methods in the class hierarchy in a depth first order.
  483. @item :c3
  484. Searches for methods in a linearized way that most closely matches
  485. what CLOS does when a monotonic class structure is defined.
  486. @end table
  487. @xref{Method Invocation}, for more on method invocation order.
  488. @item :metaclass
  489. Unsupported CLOS option. Enables the use of a different base class other
  490. than @code{standard-class}.
  491. @item :default-initargs
  492. Unsupported CLOS option. Specifies a list of initargs to be used when
  493. creating new objects. As far as I can tell, this duplicates the
  494. function of @code{:initform}.
  495. @end table
  496. @xref{CLOS compatibility}, for more details on CLOS tags versus
  497. @eieio{}-specific tags.
  498. @node Making New Objects
  499. @chapter Making New Objects
  500. Suppose we have a simple class is defined, such as:
  501. @example
  502. (defclass record ()
  503. ( ) "Doc String")
  504. @end example
  505. @noindent
  506. It is now possible to create objects of that class type.
  507. Calling @code{defclass} has defined two new functions. One is the
  508. constructor @var{record}, and the other is the predicate,
  509. @var{record}-p.
  510. @defun record object-name &rest slots
  511. This creates and returns a new object. This object is not assigned to
  512. anything, and will be garbage collected if not saved. This object
  513. will be given the string name @var{object-name}. There can be
  514. multiple objects of the same name, but the name slot provides a handy
  515. way to keep track of your objects. @var{slots} is just all the slots
  516. you wish to preset. Any slot set as such @emph{will not} get its
  517. default value, and any side effects from a slot's @code{:initform}
  518. that may be a function will not occur.
  519. An example pair would appear simply as @code{:value 1}. Of course you
  520. can do any valid Lispy thing you want with it, such as
  521. @code{:value (if (boundp 'special-symbol) special-symbol nil)}
  522. Example of creating an object from a class:
  523. @example
  524. (record :value 3 :reference nil)
  525. @end example
  526. @end defun
  527. To create an object from a class symbol, use @code{make-instance}.
  528. @defun make-instance class &rest initargs
  529. @anchor{make-instance}
  530. Make a new instance of @var{class} based on @var{initargs}.
  531. @var{class} is a class symbol. For example:
  532. @example
  533. (make-instance 'foo)
  534. @end example
  535. @var{initargs} is a property list with keywords based on the @code{:initarg}
  536. for each slot. For example:
  537. @example
  538. (make-instance @code{'foo} @code{:slot1} value1 @code{:slotN} valueN)
  539. @end example
  540. @end defun
  541. @node Accessing Slots
  542. @chapter Accessing Slots
  543. There are several ways to access slot values in an object. The naming
  544. and argument-order conventions are similar to those used for
  545. referencing vectors (@pxref{Vectors,,,elisp,GNU Emacs Lisp Reference
  546. Manual}).
  547. @defmac oset object slot value
  548. This macro sets the value behind @var{slot} to @var{value} in
  549. @var{object}. It returns @var{value}.
  550. @end defmac
  551. @defmac oset-default class slot value
  552. This macro sets the value for the class-allocated @var{slot} in @var{class} to
  553. @var{value}.
  554. For example, if a user wanted all @code{data-objects} (@pxref{Building
  555. Classes}) to inform a special object of his own devising when they
  556. changed, this can be arranged by simply executing this bit of code:
  557. @example
  558. (oset-default data-object reference (list my-special-object))
  559. @end example
  560. @end defmac
  561. @defmac oref obj slot
  562. @anchor{oref}
  563. Retrieve the value stored in @var{obj} in the slot named by @var{slot}.
  564. Slot is the name of the slot when created by @dfn{defclass}.
  565. @end defmac
  566. @defmac oref-default class slot
  567. @anchor{oref-default}
  568. Get the value of the class-allocated @var{slot} from @var{class}.
  569. @end defmac
  570. The following accessors are defined by CLOS to reference or modify
  571. slot values, and use the previously mentioned set/ref routines.
  572. @defun slot-value object slot
  573. @anchor{slot-value}
  574. This function retrieves the value of @var{slot} from @var{object}.
  575. Unlike @code{oref}, the symbol for @var{slot} must be quoted.
  576. @end defun
  577. @defun set-slot-value object slot value
  578. @anchor{set-slot-value}
  579. This is not a CLOS function, but is the setter for @code{slot-value}
  580. used by the @code{setf} macro. This
  581. function sets the value of @var{slot} from @var{object}. Unlike
  582. @code{oset}, the symbol for @var{slot} must be quoted.
  583. @end defun
  584. @defun slot-makeunbound object slot
  585. This function unbinds @var{slot} in @var{object}. Referencing an
  586. unbound slot can signal an error.
  587. @end defun
  588. @defun object-add-to-list object slot item &optional append
  589. @anchor{object-add-to-list}
  590. In OBJECT's @var{slot}, add @var{item} to the list of elements.
  591. Optional argument @var{append} indicates we need to append to the list.
  592. If @var{item} already exists in the list in @var{slot}, then it is not added.
  593. Comparison is done with @dfn{equal} through the @dfn{member} function call.
  594. If @var{slot} is unbound, bind it to the list containing @var{item}.
  595. @end defun
  596. @defun object-remove-from-list object slot item
  597. @anchor{object-remove-from-list}
  598. In OBJECT's @var{slot}, remove occurrences of @var{item}.
  599. Deletion is done with @dfn{delete}, which deletes by side effect
  600. and comparisons are done with @dfn{equal}.
  601. If @var{slot} is unbound, do nothing.
  602. @end defun
  603. @defun with-slots spec-list object &rest body
  604. @anchor{with-slots}
  605. Bind @var{spec-list} lexically to slot values in @var{object}, and execute @var{body}.
  606. This establishes a lexical environment for referring to the slots in
  607. the instance named by the given slot-names as though they were
  608. variables. Within such a context the value of the slot can be
  609. specified by using its slot name, as if it were a lexically bound
  610. variable. Both @code{setf} and @code{setq} can be used to set the value of the
  611. slot.
  612. @var{spec-list} is of a form similar to @dfn{let}. For example:
  613. @example
  614. ((VAR1 SLOT1)
  615. SLOT2
  616. SLOTN
  617. (VARN+1 SLOTN+1))
  618. @end example
  619. Where each @var{var} is the local variable given to the associated
  620. @var{slot}. A slot specified without a variable name is given a
  621. variable name of the same name as the slot.
  622. @example
  623. (defclass myclass () (x :initform 1))
  624. (setq mc (make-instance 'myclass))
  625. (with-slots (x) mc x) => 1
  626. (with-slots ((something x)) mc something) => 1
  627. @end example
  628. @end defun
  629. @node Writing Methods
  630. @chapter Writing Methods
  631. Writing a method in @eieio{} is similar to writing a function. The
  632. differences are that there are some extra options and there can be
  633. multiple definitions under the same function symbol.
  634. Where a method defines an implementation for a particular data type, a
  635. @dfn{generic method} accepts any argument, but contains no code. It
  636. is used to provide the dispatching to the defined methods. A generic
  637. method has no body, and is merely a symbol upon which methods are
  638. attached. It also provides the base documentation for what methods
  639. with that name do.
  640. @menu
  641. * Generics::
  642. * Methods::
  643. * Static Methods::
  644. @end menu
  645. @node Generics
  646. @section Generics
  647. Each @eieio{} method has one corresponding generic. This generic
  648. provides a function binding and the base documentation for the method
  649. symbol (@pxref{Symbol Components,,,elisp,GNU Emacs Lisp Reference
  650. Manual}).
  651. @defmac defgeneric method arglist [doc-string]
  652. This macro turns the (unquoted) symbol @var{method} into a function.
  653. @var{arglist} is the default list of arguments to use (not implemented
  654. yet). @var{doc-string} is the documentation used for this symbol.
  655. A generic function acts as a placeholder for methods. There is no
  656. need to call @code{defgeneric} yourself, as @code{defmethod} will call
  657. it if necessary. Currently the argument list is unused.
  658. @code{defgeneric} signals an error if you attempt to turn an existing
  659. Emacs Lisp function into a generic function.
  660. You can also create a generic method with @code{defmethod}
  661. (@pxref{Methods}). When a method is created and there is no generic
  662. method in place with that name, then a new generic will be created,
  663. and the new method will use it.
  664. @end defmac
  665. In CLOS, a generic call also be used to provide an argument list and
  666. dispatch precedence for all the arguments. In @eieio{}, dispatching
  667. only occurs for the first argument, so the @var{arglist} is not used.
  668. @node Methods
  669. @section Methods
  670. A method is a function that is executed if the first argument passed
  671. to it matches the method's class. Different @eieio{} classes may
  672. share the same method names.
  673. Methods are created with the @code{defmethod} macro, which is similar
  674. to @code{defun}.
  675. @defmac defmethod method [:before | :primary | :after | :static ] arglist [doc-string] forms
  676. @var{method} is the name of the function to create.
  677. @code{:before} and @code{:after} specify execution order (i.e., when
  678. this form is called). If neither of these symbols are present, the
  679. default priority is used (before @code{:after} and after
  680. @code{:before}); this default priority is represented in CLOS as
  681. @code{:primary}.
  682. @b{Note:} The @code{:BEFORE}, @code{:PRIMARY}, @code{:AFTER}, and
  683. @code{:STATIC} method tags were in all capital letters in previous
  684. versions of @eieio{}.
  685. @var{arglist} is the list of arguments to this method. The first
  686. argument in this list---and @emph{only} the first argument---may have
  687. a type specifier (see the example below). If no type specifier is
  688. supplied, the method applies to any object.
  689. @var{doc-string} is the documentation attached to the implementation.
  690. All method doc-strings are incorporated into the generic method's
  691. function documentation.
  692. @var{forms} is the body of the function.
  693. @end defmac
  694. @noindent
  695. In the following example, we create a method @code{mymethod} for the
  696. @code{classname} class:
  697. @example
  698. (defmethod mymethod ((obj classname) secondarg)
  699. "Doc string" )
  700. @end example
  701. @noindent
  702. This method only executes if the @var{obj} argument passed to it is an
  703. @eieio{} object of class @code{classname}.
  704. A method with no type specifier is a @dfn{default method}. If a given
  705. class has no implementation, then the default method is called when
  706. that method is used on a given object of that class.
  707. Only one default method per execution specifier (@code{:before},
  708. @code{:primary}, or @code{:after}) is allowed. If two
  709. @code{defmethod}s appear with @var{arglist}s lacking a type specifier,
  710. and having the same execution specifier, then the first implementation
  711. is replaced.
  712. When a method is called on an object, but there is no method specified
  713. for that object, but there is a method specified for object's parent
  714. class, the parent class' method is called. If there is a method
  715. defined for both, only the child's method is called. A child method
  716. may call a parent's method using @code{call-next-method}, described
  717. below.
  718. If multiple methods and default methods are defined for the same
  719. method and class, they are executed in this order:
  720. @enumerate
  721. @item method :before
  722. @item default :before
  723. @item method :primary
  724. @item default :primary
  725. @item method :after
  726. @item default :after
  727. @end enumerate
  728. If no methods exist, Emacs signals a @code{no-method-definition}
  729. error. @xref{Signals}.
  730. @defun call-next-method &rest replacement-args
  731. @anchor{call-next-method}
  732. This function calls the superclass method from a subclass method.
  733. This is the ``next method'' specified in the current method list.
  734. If @var{replacement-args} is non-@code{nil}, then use them instead of
  735. @code{eieio-generic-call-arglst}. At the top level, the generic
  736. argument list is passed in.
  737. Use @code{next-method-p} to find out if there is a next method to
  738. call.
  739. @end defun
  740. @defun next-method-p
  741. @anchor{next-method-p}
  742. Non-@code{nil} if there is a next method.
  743. Returns a list of lambda expressions which is the @code{next-method}
  744. order.
  745. @end defun
  746. At present, @eieio{} does not implement all the features of CLOS:
  747. @enumerate
  748. @item
  749. There is currently no @code{:around} tag.
  750. @item
  751. CLOS allows multiple sets of type-cast arguments, but @eieio{} only
  752. allows the first argument to be cast.
  753. @end enumerate
  754. @node Static Methods
  755. @section Static Methods
  756. Static methods do not depend on an object instance, but instead
  757. operate on a class. You can create a static method by using
  758. the @code{:static} key with @code{defmethod}.
  759. The first argument of a @code{:static} method will be a class rather than an
  760. object. Use the functions @code{oref-default} or @code{oset-default} which
  761. will work on a class.
  762. A class's @code{make-instance} method is defined as a @code{:static}
  763. method.
  764. @b{Note:} The @code{:static} keyword is unique to @eieio{}.
  765. @c TODO - Write some more about static methods here
  766. @node Method Invocation
  767. @chapter Method Invocation
  768. When classes are defined, you can specify the
  769. @code{:method-invocation-order}. This is a feature specific to EIEIO.
  770. This controls the order in which method resolution occurs for
  771. @code{:primary} methods in cases of multiple inheritance. The order
  772. affects which method is called first in a tree, and if
  773. @code{call-next-method} is used, it controls the order in which the
  774. stack of methods are run.
  775. The original EIEIO order turned out to be broken for multiple
  776. inheritance, but some programs depended on it. As such this option
  777. was added when the default invocation order was fixed to something
  778. that made more sense in that case.
  779. Valid values are:
  780. @table @code
  781. @item :breadth-first
  782. Search for methods in the class hierarchy in breadth first order.
  783. This is the default.
  784. @item :depth-first
  785. Search for methods in the class hierarchy in a depth first order.
  786. @item :c3
  787. Searches for methods in a linearized way that most closely matches
  788. what CLOS does when a monotonic class structure is defined.
  789. This is derived from the Dylan language documents by
  790. Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
  791. Retrieved from: http://192.220.96.201/dylan/linearization-oopsla96.html
  792. @end table
  793. @node Predicates
  794. @chapter Predicates and Utilities
  795. Now that we know how to create classes, access slots, and define
  796. methods, it might be useful to verify that everything is doing ok. To
  797. help with this a plethora of predicates have been created.
  798. @defun find-class symbol &optional errorp
  799. @anchor{find-class}
  800. Return the class that @var{symbol} represents.
  801. If there is no class, @code{nil} is returned if @var{errorp} is @code{nil}.
  802. If @var{errorp} is non-@code{nil}, @code{wrong-argument-type} is signaled.
  803. @end defun
  804. @defun class-p class
  805. @anchor{class-p}
  806. Return @code{t} if @var{class} is a valid class vector.
  807. @var{class} is a symbol.
  808. @end defun
  809. @defun slot-exists-p object-or-class slot
  810. @anchor{slot-exists-p}
  811. Non-@code{nil} if @var{object-or-class} has @var{slot}.
  812. @end defun
  813. @defun slot-boundp object slot
  814. @anchor{slot-boundp}
  815. Non-@code{nil} if OBJECT's @var{slot} is bound.
  816. Setting a slot's value makes it bound. Calling @dfn{slot-makeunbound} will
  817. make a slot unbound.
  818. @var{object} can be an instance or a class.
  819. @end defun
  820. @defun eieio-class-name class
  821. Return a string of the form @samp{#<class myclassname>} which should look
  822. similar to other Lisp objects like buffers and processes. Printing a
  823. class results only in a symbol.
  824. @end defun
  825. @defun class-option class option
  826. Return the value in @var{CLASS} of a given @var{OPTION}.
  827. For example:
  828. @example
  829. (class-option eieio-default-superclass :documentation)
  830. @end example
  831. Will fetch the documentation string for @code{eieio-default-superclass}.
  832. @end defun
  833. @defun eieio-object-name obj
  834. Return a string of the form @samp{#<object-class myobjname>} for @var{obj}.
  835. This should look like Lisp symbols from other parts of Emacs such as
  836. buffers and processes, and is shorter and cleaner than printing the
  837. object's vector. It is more useful to use @code{object-print} to get
  838. and object's print form, as this allows the object to add extra display
  839. information into the symbol.
  840. @end defun
  841. @defun eieio-object-class obj
  842. Returns the class symbol from @var{obj}.
  843. @end defun
  844. @defun eieio-object-class-name obj
  845. Returns the symbol of @var{obj}'s class.
  846. @end defun
  847. @defun eieio-class-parents class
  848. Returns the direct parents class of @var{class}. Returns @code{nil} if
  849. it is a superclass.
  850. @end defun
  851. @defun eieio-class-parents-fast class
  852. Just like @code{eieio-class-parents} except it is a macro and no type checking
  853. is performed.
  854. @end defun
  855. @defun eieio-class-parent class
  856. Deprecated function which returns the first parent of @var{class}.
  857. @end defun
  858. @defun eieio-class-children class
  859. Return the list of classes inheriting from @var{class}.
  860. @end defun
  861. @defun eieio-class-children-fast class
  862. Just like @code{eieio-class-children}, but with no checks.
  863. @end defun
  864. @defun same-class-p obj class
  865. Returns @code{t} if @var{obj}'s class is the same as @var{class}.
  866. @end defun
  867. @defun same-class-fast-p obj class
  868. Same as @code{same-class-p} except this is a macro and no type checking
  869. is performed.
  870. @end defun
  871. @defun object-of-class-p obj class
  872. Returns @code{t} if @var{obj} inherits anything from @var{class}. This
  873. is different from @code{same-class-p} because it checks for inheritance.
  874. @end defun
  875. @defun child-of-class-p child class
  876. Returns @code{t} if @var{child} is a subclass of @var{class}.
  877. @end defun
  878. @defun generic-p method-symbol
  879. Returns @code{t} if @code{method-symbol} is a generic function, as
  880. opposed to a regular Emacs Lisp function.
  881. @end defun
  882. @node Association Lists
  883. @chapter Association Lists
  884. Lisp offers the concept of association lists, with primitives such as
  885. @code{assoc} used to access them. The following functions can be used
  886. to manage association lists of @eieio{} objects:
  887. @defun object-assoc key slot list
  888. @anchor{object-assoc}
  889. Return an object if @var{key} is @dfn{equal} to SLOT's value of an object in @var{list}.
  890. @var{list} is a list of objects whose slots are searched.
  891. Objects in @var{list} do not need to have a slot named @var{slot}, nor does
  892. @var{slot} need to be bound. If these errors occur, those objects will
  893. be ignored.
  894. @end defun
  895. @defun object-assoc-list slot list
  896. Return an association list generated by extracting @var{slot} from all
  897. objects in @var{list}. For each element of @var{list} the @code{car} is
  898. the value of @var{slot}, and the @code{cdr} is the object it was
  899. extracted from. This is useful for generating completion tables.
  900. @end defun
  901. @defun eieio-build-class-alist &optional base-class
  902. Returns an alist of all currently defined classes. This alist is
  903. suitable for completion lists used by interactive functions to select a
  904. class. The optional argument @var{base-class} allows the programmer to
  905. select only a subset of classes which includes @var{base-class} and
  906. all its subclasses.
  907. @end defun
  908. @node Customizing
  909. @chapter Customizing Objects
  910. @eieio{} supports the Custom facility through two new widget types.
  911. If a variable is declared as type @code{object}, then full editing of
  912. slots via the widgets is made possible. This should be used
  913. carefully, however, because modified objects are cloned, so if there
  914. are other references to these objects, they will no longer be linked
  915. together.
  916. If you want in place editing of objects, use the following methods:
  917. @defun eieio-customize-object object
  918. Create a custom buffer and insert a widget for editing @var{object}. At
  919. the end, an @code{Apply} and @code{Reset} button are available. This
  920. will edit the object "in place" so references to it are also changed.
  921. There is no effort to prevent multiple edits of a singular object, so
  922. care must be taken by the user of this function.
  923. @end defun
  924. @defun eieio-custom-widget-insert object flags
  925. This method inserts an edit object into the current buffer in place.
  926. It is implemented as @code{(widget-create 'object-edit :value object)}.
  927. This method is provided as a locale for adding tracking, or
  928. specializing the widget insert procedure for any object.
  929. @end defun
  930. To define a slot with an object in it, use the @code{object} tag. This
  931. widget type will be automatically converted to @code{object-edit} if you
  932. do in place editing of you object.
  933. If you want to have additional actions taken when a user clicks on the
  934. @code{Apply} button, then overload the method @code{eieio-done-customizing}.
  935. This method does nothing by default, but that may change in the future.
  936. This would be the best way to make your objects persistent when using
  937. in-place editing.
  938. @section Widget extension
  939. When widgets are being created, one new widget extension has been added,
  940. called the @code{:slotofchoices}. When this occurs in a widget
  941. definition, all elements after it are removed, and the slot is specifies
  942. is queried and converted into a series of constants.
  943. @example
  944. (choice (const :tag "None" nil)
  945. :slotofchoices morestuff)
  946. @end example
  947. and if the slot @code{morestuff} contains @code{(sym1 sym2 sym3)}, the
  948. above example is converted into:
  949. @example
  950. (choice (const :tag "None" nil)
  951. (const sym1)
  952. (const sym2)
  953. (const sym3))
  954. @end example
  955. This is useful when a given item needs to be selected from a list of
  956. items defined in this second slot.
  957. @node Introspection
  958. @chapter Introspection
  959. Introspection permits a programmer to peek at the contents of a class
  960. without any previous knowledge of that class. While @eieio{} implements
  961. objects on top of vectors, and thus everything is technically visible,
  962. some functions have been provided. None of these functions are a part
  963. of CLOS.
  964. @defun object-slots obj
  965. Return the list of public slots for @var{obj}.
  966. @end defun
  967. @defun class-slot-initarg class slot
  968. For the given @var{class} return an :initarg associated with
  969. @var{slot}. Not all slots have initargs, so the return value can be
  970. @code{nil}.
  971. @end defun
  972. @node Base Classes
  973. @chapter Base Classes
  974. All defined classes, if created with no specified parent class,
  975. inherit from a special class called @code{eieio-default-superclass}.
  976. @xref{Default Superclass}.
  977. Often, it is more convenient to inherit from one of the other base
  978. classes provided by @eieio{}, which have useful pre-defined
  979. properties. (Since @eieio{} supports multiple inheritance, you can
  980. even inherit from more than one of these classes at once.)
  981. @menu
  982. * eieio-instance-inheritor:: Enable value inheritance between instances.
  983. * eieio-instance-tracker:: Enable self tracking instances.
  984. * eieio-singleton:: Only one instance of a given class.
  985. * eieio-persistent:: Enable persistence for a class.
  986. * eieio-named:: Use the object name as a :name slot.
  987. * eieio-speedbar:: Enable speedbar support in your objects.
  988. @end menu
  989. @node eieio-instance-inheritor
  990. @section @code{eieio-instance-inheritor}
  991. This class is defined in the package @file{eieio-base}.
  992. Instance inheritance is a mechanism whereby the value of a slot in
  993. object instance can reference the parent instance. If the parent's slot
  994. value is changed, then the child instance is also changed. If the
  995. child's slot is set, then the parent's slot is not modified.
  996. @deftp {Class} eieio-instance-inheritor parent-instance
  997. A class whose instances are enabled with instance inheritance.
  998. The @var{parent-instance} slot indicates the instance which is
  999. considered the parent of the current instance. Default is @code{nil}.
  1000. @end deftp
  1001. @cindex clone
  1002. To use this class, inherit from it with your own class.
  1003. To make a new instance that inherits from and existing instance of your
  1004. class, use the @code{clone} method with additional parameters
  1005. to specify local values.
  1006. @cindex slot-unbound
  1007. The @code{eieio-instance-inheritor} class works by causing cloned
  1008. objects to have all slots unbound. This class' @code{slot-unbound}
  1009. method will cause references to unbound slots to be redirected to the
  1010. parent instance. If the parent slot is also unbound, then
  1011. @code{slot-unbound} will signal an error named @code{slot-unbound}.
  1012. @node eieio-instance-tracker
  1013. @section @code{eieio-instance-tracker}
  1014. This class is defined in the package @file{eieio-base}.
  1015. Sometimes it is useful to keep a master list of all instances of a given
  1016. class. The class @code{eieio-instance-tracker} performs this task.
  1017. @deftp {Class} eieio-instance-tracker tracker-symbol
  1018. Enable instance tracking for this class.
  1019. The slot @var{tracker-symbol} should be initialized in inheritors of
  1020. this class to a symbol created with @code{defvar}. This symbol will
  1021. serve as the variable used as a master list of all objects of the given
  1022. class.
  1023. @end deftp
  1024. @defmethod eieio-instance-tracker initialize-instance obj slot
  1025. This method is defined as an @code{:after} method.
  1026. It adds new instances to the master list. Do not overload this method
  1027. unless you use @code{call-next-method.}
  1028. @end defmethod
  1029. @defmethod eieio-instance-tracker delete-instance obj
  1030. Remove @var{obj} from the master list of instances of this class.
  1031. This may let the garbage collector nab this instance.
  1032. @end defmethod
  1033. @deffn eieio-instance-tracker-find key slot list-symbol
  1034. This convenience function lets you find instances. @var{key} is the
  1035. value to search for. @var{slot} is the slot to compare @var{KEY}
  1036. against. The function @code{equal} is used for comparison.
  1037. The parameter @var{list-symbol} is the variable symbol which contains the
  1038. list of objects to be searched.
  1039. @end deffn
  1040. @node eieio-singleton
  1041. @section @code{eieio-singleton}
  1042. This class is defined in the package @file{eieio-base}.
  1043. @deftp {Class} eieio-singleton
  1044. Inheriting from the singleton class will guarantee that there will
  1045. only ever be one instance of this class. Multiple calls to
  1046. @code{make-instance} will always return the same object.
  1047. @end deftp
  1048. @node eieio-persistent
  1049. @section @code{eieio-persistent}
  1050. This class is defined in the package @file{eieio-base}.
  1051. If you want an object, or set of objects to be persistent, meaning the
  1052. slot values are important to keep saved between sessions, then you will
  1053. want your top level object to inherit from @code{eieio-persistent}.
  1054. To make sure your persistent object can be moved, make sure all file
  1055. names stored to disk are made relative with
  1056. @code{eieio-persistent-path-relative}.
  1057. @deftp {Class} eieio-persistent file file-header-line
  1058. Enables persistence for instances of this class.
  1059. Slot @var{file} with initarg @code{:file} is the file name in which this
  1060. object will be saved.
  1061. Class allocated slot @var{file-header-line} is used with method
  1062. @code{object-write} as a header comment.
  1063. @end deftp
  1064. All objects can write themselves to a file, but persistent objects have
  1065. several additional methods that aid in maintaining them.
  1066. @defmethod eieio-persistent eieio-persistent-save obj &optional file
  1067. Write the object @var{obj} to its file.
  1068. If optional argument @var{file} is specified, use that file name
  1069. instead.
  1070. @end defmethod
  1071. @defmethod eieio-persistent eieio-persistent-path-relative obj file
  1072. Return a file name derived from @var{file} which is relative to the
  1073. stored location of @var{OBJ}. This method should be used to convert
  1074. file names so that they are relative to the save file, making any system
  1075. of files movable from one location to another.
  1076. @end defmethod
  1077. @defmethod eieio-persistent object-write obj &optional comment
  1078. Like @code{object-write} for @code{standard-object}, but will derive
  1079. a header line comment from the class allocated slot if one is not
  1080. provided.
  1081. @end defmethod
  1082. @defun eieio-persistent-read filename &optional class allow-subclass
  1083. Read a persistent object from @var{filename}, and return it.
  1084. Signal an error if the object in @var{FILENAME} is not a constructor
  1085. for @var{CLASS}. Optional @var{allow-subclass} says that it is ok for
  1086. @code{eieio-persistent-read} to load in subclasses of class instead of
  1087. being pedantic.
  1088. @end defun
  1089. @node eieio-named
  1090. @section @code{eieio-named}
  1091. This class is defined in the package @file{eieio-base}.
  1092. @deftp {Class} eieio-named
  1093. Object with a name.
  1094. Name storage already occurs in an object. This object provides get/set
  1095. access to it.
  1096. @end deftp
  1097. @node eieio-speedbar
  1098. @section @code{eieio-speedbar}
  1099. This class is in package @file{eieio-speedbar}.
  1100. If a series of class instances map to a tree structure, it is possible
  1101. to cause your classes to be displayable in Speedbar. @xref{Top,,,speedbar}.
  1102. Inheriting from these classes will enable a speedbar major display mode
  1103. with a minimum of effort.
  1104. @deftp {Class} eieio-speedbar buttontype buttonface
  1105. Enables base speedbar display for a class.
  1106. @cindex speedbar-make-tag-line
  1107. The slot @var{buttontype} is any of the symbols allowed by the
  1108. function @code{speedbar-make-tag-line} for the @var{exp-button-type}
  1109. argument @xref{Extending,,,speedbar}.
  1110. The slot @var{buttonface} is the face to use for the text of the string
  1111. displayed in speedbar.
  1112. The slots @var{buttontype} and @var{buttonface} are class allocated
  1113. slots, and do not take up space in your instances.
  1114. @end deftp
  1115. @deftp {Class} eieio-speedbar-directory-button buttontype buttonface
  1116. This class inherits from @code{eieio-speedbar} and initializes
  1117. @var{buttontype} and @var{buttonface} to appear as directory level lines.
  1118. @end deftp
  1119. @deftp {Class} eieio-speedbar-file-button buttontype buttonface
  1120. This class inherits from @code{eieio-speedbar} and initializes
  1121. @var{buttontype} and @var{buttonface} to appear as file level lines.
  1122. @end deftp
  1123. To use these classes, inherit from one of them in you class. You can
  1124. use multiple inheritance with them safely. To customize your class for
  1125. speedbar display, override the default values for @var{buttontype} and
  1126. @var{buttonface} to get the desired effects.
  1127. Useful methods to define for your new class include:
  1128. @defmethod eieio-speedbar eieio-speedbar-derive-line-path obj depth
  1129. Return a string representing a directory associated with an instance
  1130. of @var{obj}. @var{depth} can be used to index how many levels of
  1131. indentation have been opened by the user where @var{obj} is shown.
  1132. @end defmethod
  1133. @defmethod eieio-speedbar eieio-speedbar-description obj
  1134. Return a string description of @var{OBJ}.
  1135. This is shown in the minibuffer or tooltip when the mouse hovers over
  1136. this instance in speedbar.
  1137. @end defmethod
  1138. @defmethod eieio-speedbar eieio-speedbar-child-description obj
  1139. Return a string representing a description of a child node of @var{obj}
  1140. when that child is not an object. It is often useful to just use
  1141. item info helper functions such as @code{speedbar-item-info-file-helper}.
  1142. @end defmethod
  1143. @defmethod eieio-speedbar eieio-speedbar-object-buttonname obj
  1144. Return a string which is the text displayed in speedbar for @var{obj}.
  1145. @end defmethod
  1146. @defmethod eieio-speedbar eieio-speedbar-object-children obj
  1147. Return a list of children of @var{obj}.
  1148. @end defmethod
  1149. @defmethod eieio-speedbar eieio-speedbar-child-make-tag-lines obj depth
  1150. This method inserts a list of speedbar tag lines for @var{obj} to
  1151. represent its children. Implement this method for your class
  1152. if your children are not objects themselves. You still need to
  1153. implement @code{eieio-speedbar-object-children}.
  1154. In this method, use techniques specified in the Speedbar manual.
  1155. @xref{Extending,,,speedbar}.
  1156. @end defmethod
  1157. Some other functions you will need to learn to use are:
  1158. @deffn eieio-speedbar-create make-map key-map menu name toplevelfn
  1159. Register your object display mode with speedbar.
  1160. @var{make-map} is a function which initialized you keymap.
  1161. @var{key-map} is a symbol you keymap is installed into.
  1162. @var{menu} is an easy menu vector representing menu items specific to your
  1163. object display.
  1164. @var{name} is a short string to use as a name identifying you mode.
  1165. @var{toplevelfn} is a function called which must return a list of
  1166. objects representing those in the instance system you wish to browse in
  1167. speedbar.
  1168. Read the Extending chapter in the speedbar manual for more information
  1169. on how speedbar modes work
  1170. @xref{Extending,,,speedbar}.
  1171. @end deffn
  1172. @node Browsing
  1173. @chapter Browsing class trees
  1174. The command @kbd{M-x eieio-browse} displays a buffer listing all the
  1175. currently loaded classes in Emacs. The classes are listed in an
  1176. indented tree structure, starting from @code{eieio-default-superclass}
  1177. (@pxref{Default Superclass}).
  1178. With a prefix argument, this command prompts for a class name; it then
  1179. lists only that class and its subclasses.
  1180. Here is a sample tree from our current example:
  1181. @example
  1182. eieio-default-superclass
  1183. +--data-object
  1184. +--data-object-symbol
  1185. @end example
  1186. Note: new classes are consed into the inheritance lists, so the tree
  1187. comes out upside-down.
  1188. @node Class Values
  1189. @chapter Class Values
  1190. You can use the normal @code{describe-function} command to retrieve
  1191. information about a class. Running it on constructors will show a
  1192. full description of the generated class. If you call it on a generic
  1193. function, all implementations of that generic function will be listed,
  1194. together with links through which you can directly jump to the source.
  1195. @node Default Superclass
  1196. @chapter Default Superclass
  1197. All defined classes, if created with no specified parent class, will
  1198. inherit from a special class stored in
  1199. @code{eieio-default-superclass}. This superclass is quite simple, but
  1200. with it, certain default methods or attributes can be added to all
  1201. objects. In CLOS, this would be named @code{STANDARD-CLASS}, and that
  1202. symbol is an alias to @code{eieio-default-superclass}.
  1203. Currently, the default superclass is defined as follows:
  1204. @example
  1205. (defclass eieio-default-superclass nil
  1206. nil
  1207. "Default parent class for classes with no specified parent class.
  1208. Its slots are automatically adopted by classes with no specified
  1209. parents. This class is not stored in the `parent' slot of a class vector."
  1210. :abstract t)
  1211. @end example
  1212. The default superclass implements several methods providing a default
  1213. behavior for all objects created by @eieio{}.
  1214. @menu
  1215. * Initialization:: How objects are initialized
  1216. * Basic Methods:: Clone, print, and write
  1217. * Signal Handling:: Methods for managing signals.
  1218. @end menu
  1219. @node Initialization
  1220. @section Initialization
  1221. When creating an object of any type, you can use its constructor, or
  1222. @code{make-instance}. This, in turns calls the method
  1223. @code{initialize-instance}, which then calls the method
  1224. @code{shared-initialize}.
  1225. These methods are all implemented on the default superclass so you do
  1226. not need to write them yourself, unless you need to override one of
  1227. their behaviors.
  1228. Users should not need to call @code{initialize-instance} or
  1229. @code{shared-initialize}, as these are used by @code{make-instance} to
  1230. initialize the object. They are instead provided so that users can
  1231. augment these behaviors.
  1232. @defun initialize-instance obj &rest slots
  1233. Initialize @var{obj}. Sets slots of @var{obj} with @var{slots} which
  1234. is a list of name/value pairs. These are actually just passed to
  1235. @code{shared-initialize}.
  1236. @end defun
  1237. @defun shared-initialize obj &rest slots
  1238. Sets slots of @var{obj} with @var{slots} which is a list of name/value
  1239. pairs.
  1240. This is called from the default constructor.
  1241. @end defun
  1242. @node Basic Methods
  1243. @section Basic Methods
  1244. Additional useful methods defined on the base subclass are:
  1245. @defun clone obj &rest params
  1246. @anchor{clone}
  1247. Make a copy of @var{obj}, and then apply @var{params}.
  1248. @var{params} is a parameter list of the same form as @var{initialize-instance}
  1249. which are applied to change the object. When overloading @dfn{clone}, be
  1250. sure to call @dfn{call-next-method} first and modify the returned object.
  1251. @end defun
  1252. @defun object-print this &rest strings
  1253. @anchor{object-print}
  1254. Pretty printer for object @var{this}. Call function @dfn{eieio-object-name} with @var{strings}.
  1255. The default method for printing object @var{this} is to use the
  1256. function @dfn{eieio-object-name}.
  1257. It is sometimes useful to put a summary of the object into the
  1258. default #<notation> string when using eieio browsing tools.
  1259. Implement this function and specify @var{strings} in a call to
  1260. @dfn{call-next-method} to provide additional summary information.
  1261. When passing in extra strings from child classes, always remember
  1262. to prepend a space.
  1263. @example
  1264. (defclass data-object ()
  1265. (value)
  1266. "Object containing one data slot.")
  1267. (defmethod object-print ((this data-object) &optional strings)
  1268. "Return a string with a summary of the data object as part of the name."
  1269. (apply 'call-next-method this
  1270. (cons (format " value: %s" (render this)) strings)))
  1271. @end example
  1272. Here is what some output could look like:
  1273. @example
  1274. (object-print test-object)
  1275. => #<data-object test-object value: 3>
  1276. @end example
  1277. @end defun
  1278. @defun object-write obj &optional comment
  1279. Write @var{obj} onto a stream in a readable fashion. The resulting
  1280. output will be Lisp code which can be used with @code{read} and
  1281. @code{eval} to recover the object. Only slots with @code{:initarg}s
  1282. are written to the stream.
  1283. @end defun
  1284. @node Signal Handling
  1285. @section Signal Handling
  1286. The default superclass defines methods for managing error conditions.
  1287. These methods all throw a signal for a particular error condition.
  1288. By implementing one of these methods for a class, you can change the
  1289. behavior that occurs during one of these error cases, or even ignore
  1290. the error by providing some behavior.
  1291. @defun slot-missing object slot-name operation &optional new-value
  1292. @anchor{slot-missing}
  1293. Method invoked when an attempt to access a slot in @var{object} fails.
  1294. @var{slot-name} is the name of the failed slot, @var{operation} is the type of access
  1295. that was requested, and optional @var{new-value} is the value that was desired
  1296. to be set.
  1297. This method is called from @code{oref}, @code{oset}, and other functions which
  1298. directly reference slots in EIEIO objects.
  1299. The default method signals an error of type @code{invalid-slot-name}.
  1300. @xref{Signals}.
  1301. You may override this behavior, but it is not expected to return in the
  1302. current implementation.
  1303. This function takes arguments in a different order than in CLOS.
  1304. @end defun
  1305. @defun slot-unbound object class slot-name fn
  1306. @anchor{slot-unbound}
  1307. Slot unbound is invoked during an attempt to reference an unbound slot.
  1308. @var{object} is the instance of the object being reference. @var{class} is the
  1309. class of @var{object}, and @var{slot-name} is the offending slot. This function
  1310. throws the signal @code{unbound-slot}. You can overload this function and
  1311. return the value to use in place of the unbound value.
  1312. Argument @var{fn} is the function signaling this error.
  1313. Use @dfn{slot-boundp} to determine if a slot is bound or not.
  1314. In @var{clos}, the argument list is (@var{class} @var{object} @var{slot-name}), but
  1315. @var{eieio} can only dispatch on the first argument, so the first two are swapped.
  1316. @end defun
  1317. @defun no-applicable-method object method &rest args
  1318. @anchor{no-applicable-method}
  1319. Called if there are no implementations for @var{object} in @var{method}.
  1320. @var{object} is the object which has no method implementation.
  1321. @var{args} are the arguments that were passed to @var{method}.
  1322. Implement this for a class to block this signal. The return
  1323. value becomes the return value of the original method call.
  1324. @end defun
  1325. @defun no-next-method object &rest args
  1326. @anchor{no-next-method}
  1327. Called from @dfn{call-next-method} when no additional methods are available.
  1328. @var{object} is othe object being called on @dfn{call-next-method}.
  1329. @var{args} are the arguments it is called by.
  1330. This method signals @dfn{no-next-method} by default. Override this
  1331. method to not throw an error, and its return value becomes the
  1332. return value of @dfn{call-next-method}.
  1333. @end defun
  1334. @node Signals
  1335. @chapter Signals
  1336. There are new condition names (signals) that can be caught when using
  1337. @eieio{}.
  1338. @deffn Signal invalid-slot-name obj-or-class slot
  1339. This signal is called when an attempt to reference a slot in an
  1340. @var{obj-or-class} is made, and the @var{slot} is not defined for
  1341. it.
  1342. @end deffn
  1343. @deffn Signal no-method-definition method arguments
  1344. This signal is called when @var{method} is called, with @var{arguments}
  1345. and nothing is resolved. This occurs when @var{method} has been
  1346. defined, but the arguments make it impossible for @eieio{} to determine
  1347. which method body to run.
  1348. To prevent this signal from occurring in your class, implement the
  1349. method @code{no-applicable-method} for your class. This method is
  1350. called when to throw this signal, so implementing this for your class
  1351. allows you block the signal, and perform some work.
  1352. @end deffn
  1353. @deffn Signal no-next-method class arguments
  1354. This signal is called if the function @code{call-next-method} is called
  1355. and there is no next method to be called.
  1356. Overload the method @code{no-next-method} to protect against this signal.
  1357. @end deffn
  1358. @deffn Signal invalid-slot-type slot spec value
  1359. This signal is called when an attempt to set @var{slot} is made, and
  1360. @var{value} doesn't match the specified type @var{spec}.
  1361. In @eieio{}, this is also used if a slot specifier has an invalid value
  1362. during a @code{defclass}.
  1363. @end deffn
  1364. @deffn Signal unbound-slot object class slot
  1365. This signal is called when an attempt to reference @var{slot} in
  1366. @var{object} is made, and that instance is currently unbound.
  1367. @end deffn
  1368. @node Naming Conventions
  1369. @chapter Naming Conventions
  1370. @xref{Tips,,Tips and Conventions,elisp,GNU Emacs Lisp Reference
  1371. Manual}, for a description of Emacs Lisp programming conventions.
  1372. These conventions help ensure that Emacs packages work nicely one
  1373. another, so an @eieio{}-based program should follow them. Here are
  1374. some conventions that apply specifically to @eieio{}-based programs:
  1375. @itemize
  1376. @item Come up with a package prefix that is relatively short. Prefix
  1377. all classes, and methods with your prefix. This is a standard
  1378. convention for functions and variables in Emacs.
  1379. @item Do not prefix method names with the class name. All methods in
  1380. @eieio{} are ``virtual'', and are dynamically dispatched. Anyone can
  1381. override your methods at any time. Your methods should be prefixed
  1382. with your package name.
  1383. @item Do not prefix slots in your class. The slots are always locally
  1384. scoped to your class, and need no prefixing.
  1385. @item If your library inherits from other libraries of classes, you
  1386. must ``require'' that library with the @code{require} command.
  1387. @end itemize
  1388. @node CLOS compatibility
  1389. @chapter CLOS compatibility
  1390. Currently, the following functions should behave almost as expected from
  1391. CLOS.
  1392. @table @code
  1393. @item defclass
  1394. All slot keywords are available but not all work correctly.
  1395. Slot keyword differences are:
  1396. @table @asis
  1397. @item :reader, and :writer tags
  1398. Create methods that signal errors instead of creating an unqualified
  1399. method. You can still create new ones to do its business.
  1400. @item :accessor
  1401. This should create an unqualified method to access a slot, but
  1402. instead pre-builds a method that gets the slot's value.
  1403. @item :type
  1404. Specifier uses the @code{typep} function from the @file{cl}
  1405. package. @xref{Type Predicates,,,cl,Common Lisp Extensions}.
  1406. It therefore has the same issues as that package. Extensions include
  1407. the ability to provide object names.
  1408. @end table
  1409. defclass also supports class options, but does not currently use values
  1410. of @code{:metaclass}, and @code{:default-initargs}.
  1411. @item make-instance
  1412. Make instance works as expected, however it just uses the @eieio{} instance
  1413. creator automatically generated when a new class is created.
  1414. @xref{Making New Objects}.
  1415. @item defgeneric
  1416. Creates the desired symbol, and accepts all of the expected arguments
  1417. except @code{:around}.
  1418. @item defmethod
  1419. Calls defgeneric, and accepts most of the expected arguments. Only
  1420. the first argument to the created method may have a type specifier.
  1421. To type cast against a class, the class must exist before defmethod is
  1422. called. In addition, the @code{:around} tag is not supported.
  1423. @item call-next-method
  1424. Inside a method, calls the next available method up the inheritance tree
  1425. for the given object. This is different than that found in CLOS because
  1426. in @eieio{} this function accepts replacement arguments. This permits
  1427. subclasses to modify arguments as they are passed up the tree. If no
  1428. arguments are given, the expected CLOS behavior is used.
  1429. @end table
  1430. CLOS supports the @code{describe} command, but @eieio{} provides
  1431. support for using the standard @code{describe-function} command on a
  1432. constructor or generic function.
  1433. When creating a new class (@pxref{Building Classes}) there are several
  1434. new keywords supported by @eieio{}.
  1435. In @eieio{} tags are in lower case, not mixed case.
  1436. @node Wish List
  1437. @chapter Wish List
  1438. @eieio{} is an incomplete implementation of CLOS@. Finding ways to
  1439. improve the compatibility would help make CLOS style programs run
  1440. better in Emacs.
  1441. Some important compatibility features that would be good to add are:
  1442. @enumerate
  1443. @item
  1444. Support for metaclasses and EQL specialization.
  1445. @item
  1446. @code{:around} method key.
  1447. @item
  1448. Method dispatch for built-in types.
  1449. @item
  1450. Method dispatch for multiple argument typing.
  1451. @item
  1452. Improve integration with the @file{cl} package.
  1453. @end enumerate
  1454. There are also improvements to be made to allow @eieio{} to operate
  1455. better in the Emacs environment.
  1456. @enumerate
  1457. @item
  1458. Allow subclassing of Emacs built-in types, such as faces, markers, and
  1459. buffers.
  1460. @item
  1461. Allow method overloading of method-like functions in Emacs.
  1462. @end enumerate
  1463. @node GNU Free Documentation License
  1464. @appendix GNU Free Documentation License
  1465. @include doclicense.texi
  1466. @node Function Index
  1467. @unnumbered Function Index
  1468. @printindex fn
  1469. @contents
  1470. @bye