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