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