aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorStefan Monnier2017-02-28 11:11:01 -0500
committerStefan Monnier2017-02-28 11:11:01 -0500
commit0fa594172709770a431121360f8f122df14d6259 (patch)
tree10775b677d5899dda45ee24976793973becbf889 /doc
parent8d7a3f489082e2aaf4ff238613a497cc03d833ae (diff)
downloademacs-0fa594172709770a431121360f8f122df14d6259.tar.gz
emacs-0fa594172709770a431121360f8f122df14d6259.zip
* doc/misc/eieio.texi: Update to account for the cl-generic facilities
(Quick Start, Class Options, Generics): Adjust names for cl-generic. (Methods): Document cl-defmethod. Explain in more detail the order in which the various methods are executed. Document the conditions under which a method is redefined. Remove reference to `eieio-generic-call-arglst`. Don't document the precise return value of cl-next-method-p. (Static Methods): Adjust to use `subclass` specializer. (Method Invocation): Use cl-call-next-method and drop mention of :primary. (Signal Handling, Signals): Adjust names and args for cl-generic; add cl-no-primary-method. (CLOS compatibility, Wish List): Adjust to new featureset.
Diffstat (limited to 'doc')
-rw-r--r--doc/misc/eieio.texi238
1 files changed, 121 insertions, 117 deletions
diff --git a/doc/misc/eieio.texi b/doc/misc/eieio.texi
index 3820bd50dfc..ce31bc84b4e 100644
--- a/doc/misc/eieio.texi
+++ b/doc/misc/eieio.texi
@@ -112,7 +112,7 @@ three slots named @code{name}, @code{birthday}, and @code{phone}:
112Each class can have methods, which are defined like this: 112Each class can have methods, which are defined like this:
113 113
114@example 114@example
115(defmethod call-record ((rec record) &optional scriptname) 115(cl-defmethod call-record ((rec record) &optional scriptname)
116 "Dial the phone for the record REC. 116 "Dial the phone for the record REC.
117Execute the program SCRIPTNAME to dial the phone." 117Execute the program SCRIPTNAME to dial the phone."
118 (message "Dialing the phone for %s" (oref rec name)) 118 (message "Dialing the phone for %s" (oref rec name))
@@ -170,7 +170,7 @@ or
170In these examples, @eieio{} automatically examines the class of 170In these examples, @eieio{} automatically examines the class of
171@code{rec}, and ensures that the method defined above is called. If 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 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} 173some other data type, Emacs signals a @code{cl-no-applicable-method}
174error. @ref{Signals}. 174error. @ref{Signals}.
175 175
176@node Introduction 176@node Introduction
@@ -589,9 +589,9 @@ This option is specific to Emacs, and is not in the CLOS spec.
589 589
590@item :method-invocation-order 590@item :method-invocation-order
591This controls the order in which method resolution occurs for 591This controls the order in which method resolution occurs for
592@code{:primary} methods in cases of multiple inheritance. The order 592methods in cases of multiple inheritance. The order
593affects which method is called first in a tree, and if 593affects which method is called first in a tree, and if
594@code{call-next-method} is used, it controls the order in which the 594@code{cl-call-next-method} is used, it controls the order in which the
595stack of methods are run. 595stack of methods are run.
596 596
597Valid values are: 597Valid values are:
@@ -817,19 +817,19 @@ provides a function binding and the base documentation for the method
817symbol (@pxref{Symbol Components,,,elisp,GNU Emacs Lisp Reference 817symbol (@pxref{Symbol Components,,,elisp,GNU Emacs Lisp Reference
818Manual}). 818Manual}).
819 819
820@defmac defgeneric method arglist [doc-string] 820@defmac cl-defgeneric method arglist [doc-string]
821This macro turns the (unquoted) symbol @var{method} into a function. 821This macro turns the (unquoted) symbol @var{method} into a function.
822@var{arglist} is the default list of arguments to use (not implemented 822@var{arglist} is the default list of arguments to use (not implemented
823yet). @var{doc-string} is the documentation used for this symbol. 823yet). @var{doc-string} is the documentation used for this symbol.
824 824
825A generic function acts as a placeholder for methods. There is no 825A generic function acts as a placeholder for methods. There is no
826need to call @code{defgeneric} yourself, as @code{defmethod} will call 826need to call @code{cl-defgeneric} yourself, as @code{cl-defmethod} will call
827it if necessary. Currently the argument list is unused. 827it if necessary. Currently the argument list is unused.
828 828
829@code{defgeneric} signals an error if you attempt to turn an existing 829@code{cl-defgeneric} signals an error if you attempt to turn an existing
830Emacs Lisp function into a generic function. 830Emacs Lisp function into a generic function.
831 831
832You can also create a generic method with @code{defmethod} 832You can also create a generic method with @code{cl-defmethod}
833(@pxref{Methods}). When a method is created and there is no generic 833(@pxref{Methods}). When a method is created and there is no generic
834method in place with that name, then a new generic will be created, 834method in place with that name, then a new generic will be created,
835and the new method will use it. 835and the new method will use it.
@@ -842,31 +842,26 @@ only occurs for the first argument, so the @var{arglist} is not used.
842@node Methods 842@node Methods
843@section Methods 843@section Methods
844 844
845A method is a function that is executed if the first argument passed 845A method is a function that is executed if the arguments passed
846to it matches the method's class. Different @eieio{} classes may 846to it matches the method's specializers. Different @eieio{} classes may
847share the same method names. 847share the same method names.
848 848
849Methods are created with the @code{defmethod} macro, which is similar 849Methods are created with the @code{cl-defmethod} macro, which is similar
850to @code{defun}. 850to @code{defun}.
851 851
852@defmac defmethod method [:before | :primary | :after | :static ] arglist [doc-string] forms 852@defmac cl-defmethod method [:before | :around | :after ] arglist [doc-string] forms
853 853
854@var{method} is the name of the function to create. 854@var{method} is the name of the function to create.
855 855
856@code{:before} and @code{:after} specify execution order (i.e., when 856@code{:before}, @code{:around}, and @code{:after} specify execution order
857this form is called). If neither of these symbols are present, the 857(i.e., when this form is called). If none of these symbols are present, the
858default priority is used (before @code{:after} and after 858method is said to be a @emph{primary}.
859@code{:before}); this default priority is represented in CLOS as
860@code{:primary}.
861 859
862@b{Note:} The @code{:BEFORE}, @code{:PRIMARY}, @code{:AFTER}, and 860@var{arglist} is the list of arguments to this method. The mandatory arguments
863@code{:STATIC} method tags were in all capital letters in previous 861in this list may have a type specializer (see the example below) which means
864versions of @eieio{}. 862that the method will only apply when those arguments match the given type
865 863specializer. An argument with no type specializer means that the method
866@var{arglist} is the list of arguments to this method. The first 864applies regardless of its value.
867argument in this list---and @emph{only} the first argument---may have
868a type specifier (see the example below). If no type specifier is
869supplied, the method applies to any object.
870 865
871@var{doc-string} is the documentation attached to the implementation. 866@var{doc-string} is the documentation attached to the implementation.
872All method doc-strings are incorporated into the generic method's 867All method doc-strings are incorporated into the generic method's
@@ -881,7 +876,7 @@ In the following example, we create a method @code{mymethod} for the
881@code{classname} class: 876@code{classname} class:
882 877
883@example 878@example
884(defmethod mymethod ((obj classname) secondarg) 879(cl-defmethod mymethod ((obj classname) secondarg)
885 "Doc string" ) 880 "Doc string" )
886@end example 881@end example
887 882
@@ -889,84 +884,86 @@ In the following example, we create a method @code{mymethod} for the
889This method only executes if the @var{obj} argument passed to it is an 884This method only executes if the @var{obj} argument passed to it is an
890@eieio{} object of class @code{classname}. 885@eieio{} object of class @code{classname}.
891 886
892A method with no type specifier is a @dfn{default method}. If a given 887A method with no type specializer is a @dfn{default method}. If a given
893class has no implementation, then the default method is called when 888class has no implementation, then the default method is called when
894that method is used on a given object of that class. 889that method is used on a given object of that class.
895 890
896Only one default method per execution specifier (@code{:before}, 891Only one method per combination of specializers and qualifiers (@code{:before},
897@code{:primary}, or @code{:after}) is allowed. If two 892@code{:around}, or @code{:after}) is kept. If two @code{cl-defmethod}s appear
898@code{defmethod}s appear with @var{arglist}s lacking a type specifier, 893with the same specializers and the same qualifiers, then the second
899and having the same execution specifier, then the first implementation 894implementation replaces the first.
900is replaced.
901 895
902When a method is called on an object, but there is no method specified 896When a method is called on an object, but there is no method specified
903for that object, but there is a method specified for object's parent 897for that object, but there is a method specified for object's parent
904class, the parent class' method is called. If there is a method 898class, the parent class's method is called. If there is a method
905defined for both, only the child's method is called. A child method 899defined for both, only the child's method is called. A child method
906may call a parent's method using @code{call-next-method}, described 900may call a parent's method using @code{cl-call-next-method}, described
907below. 901below.
908 902
909If multiple methods and default methods are defined for the same 903If multiple methods and default methods are defined for the same
910method and class, they are executed in this order: 904method and class, they are executed in this order:
911 905
912@enumerate 906@enumerate
913@item method :before 907@item :around methods
914@item default :before 908The most specific @code{:around} method is called first, which may invoke the
915@item method :primary 909less specific ones via @code{cl-call-next-method}. If it doesn't invoke
916@item default :primary 910@code{cl-call-next-method}, then no other methods will be executed. When there
917@item method :after 911are no more @code{:around} methods to call, falls through to run the other
918@item default :after 912(non-@code{:around}) methods.
913@item :before methods
914Called in sequence from most specific to least specific.
915@item primary methods
916The most specific method is called, which may invoke the less specific
917ones via @code{cl-call-next-method}.
918@item :after methods
919Called in sequence from least specific to most specific.
919@end enumerate 920@end enumerate
920 921
921If no methods exist, Emacs signals a @code{no-method-definition} 922If no methods exist, Emacs signals a @code{cl-no-applicable-method} error.
922error. @xref{Signals}. 923@xref{Signals}. If methods exist but none of them are primary, Emacs
924signals a @code{cl-no-primary-method} error. @xref{Signals}.
923 925
924@defun call-next-method &rest replacement-args 926@defun cl-call-next-method &rest replacement-args
925@anchor{call-next-method} 927@anchor{cl-call-next-method}
926 928
927This function calls the superclass method from a subclass method. 929This function calls the superclass method from a subclass method.
928This is the ``next method'' specified in the current method list. 930This is the ``next method'' specified in the current method list.
929 931
930If @var{replacement-args} is non-@code{nil}, then use them instead of 932If @var{replacement-args} is non-@code{nil}, then use them instead of the
931@code{eieio-generic-call-arglst}. At the top level, the generic 933arguments originally provided to the method.
932argument list is passed in.
933 934
934Use @code{next-method-p} to find out if there is a next method to 935Can only be used from within the lexical body of a primary or around method.
935call.
936@end defun 936@end defun
937 937
938@defun next-method-p 938@defun cl-next-method-p
939@anchor{next-method-p} 939@anchor{cl-next-method-p}
940Non-@code{nil} if there is a next method. 940Non-@code{nil} if there is a next method.
941Returns a list of lambda expressions which is the @code{next-method}
942order.
943@end defun
944
945At present, @eieio{} does not implement all the features of CLOS:
946 941
947@enumerate 942Can only be used from within the lexical body of a primary or around method.
948@item 943@end defun
949There is currently no @code{:around} tag.
950@item
951CLOS allows multiple sets of type-cast arguments, but @eieio{} only
952allows the first argument to be cast.
953@end enumerate
954 944
955@node Static Methods 945@node Static Methods
956@section Static Methods 946@section Static Methods
957 947
958Static methods do not depend on an object instance, but instead 948Static methods do not depend on an object instance, but instead
959operate on a class. You can create a static method by using 949operate on a class. You can create a static method by using
960the @code{:static} key with @code{defmethod}. 950the @code{subclass} specializer with @code{cl-defmethod}:
951
952@example
953(cl-defmethod make-instance ((class (subclass mychild)) &rest args)
954 (let ((new (cl-call-next-method)))
955 (push new all-my-children)
956 new))
957@end example
961 958
962The first argument of a @code{:static} method will be a class rather than an 959The first argument of a static method will be a class rather than an
963object. Use the functions @code{oref-default} or @code{oset-default} which 960object. Use the functions @code{oref-default} or @code{oset-default} which
964will work on a class. 961will work on a class.
965 962
966A class's @code{make-instance} method is defined as a @code{:static} 963A class's @code{make-instance} method is defined as a static
967method. 964method.
968 965
969@b{Note:} The @code{:static} keyword is unique to @eieio{}. 966@b{Note:} The @code{subclass} specializer is unique to @eieio{}.
970 967
971@c TODO - Write some more about static methods here 968@c TODO - Write some more about static methods here
972 969
@@ -977,9 +974,9 @@ When classes are defined, you can specify the
977@code{:method-invocation-order}. This is a feature specific to EIEIO. 974@code{:method-invocation-order}. This is a feature specific to EIEIO.
978 975
979This controls the order in which method resolution occurs for 976This controls the order in which method resolution occurs for
980@code{:primary} methods in cases of multiple inheritance. The order 977methods in cases of multiple inheritance. The order
981affects which method is called first in a tree, and if 978affects which method is called first in a tree, and if
982@code{call-next-method} is used, it controls the order in which the 979@code{cl-call-next-method} is used, it controls the order in which the
983stack of methods are run. 980stack of methods are run.
984 981
985The original EIEIO order turned out to be broken for multiple 982The original EIEIO order turned out to be broken for multiple
@@ -1297,8 +1294,7 @@ class.
1297 1294
1298@defmethod eieio-instance-tracker initialize-instance obj slot 1295@defmethod eieio-instance-tracker initialize-instance obj slot
1299This method is defined as an @code{:after} method. 1296This method is defined as an @code{:after} method.
1300It adds new instances to the master list. Do not overload this method 1297It adds new instances to the master list.
1301unless you use @code{call-next-method.}
1302@end defmethod 1298@end defmethod
1303 1299
1304@defmethod eieio-instance-tracker delete-instance obj 1300@defmethod eieio-instance-tracker delete-instance obj
@@ -1582,7 +1578,7 @@ Additional useful methods defined on the base subclass are:
1582Make a copy of @var{obj}, and then apply @var{params}. 1578Make a copy of @var{obj}, and then apply @var{params}.
1583@var{params} is a parameter list of the same form as @var{initialize-instance} 1579@var{params} is a parameter list of the same form as @var{initialize-instance}
1584which are applied to change the object. When overloading @dfn{clone}, be 1580which are applied to change the object. When overloading @dfn{clone}, be
1585sure to call @dfn{call-next-method} first and modify the returned object. 1581sure to call @dfn{cl-call-next-method} first and modify the returned object.
1586@end defun 1582@end defun
1587 1583
1588@defun object-print this &rest strings 1584@defun object-print this &rest strings
@@ -1595,7 +1591,7 @@ It is sometimes useful to put a summary of the object into the
1595default #<notation> string when using eieio browsing tools. 1591default #<notation> string when using eieio browsing tools.
1596 1592
1597Implement this function and specify @var{strings} in a call to 1593Implement this function and specify @var{strings} in a call to
1598@dfn{call-next-method} to provide additional summary information. 1594@dfn{cl-call-next-method} to provide additional summary information.
1599When passing in extra strings from child classes, always remember 1595When passing in extra strings from child classes, always remember
1600to prepend a space. 1596to prepend a space.
1601 1597
@@ -1604,10 +1600,11 @@ to prepend a space.
1604 (value) 1600 (value)
1605 "Object containing one data slot.") 1601 "Object containing one data slot.")
1606 1602
1607(defmethod object-print ((this data-object) &optional strings) 1603(cl-defmethod object-print ((this data-object) &optional strings)
1608 "Return a string with a summary of the data object as part of the name." 1604 "Return a string with a summary of the data object as part of the name."
1609 (apply 'call-next-method this 1605 (apply #'cl-call-next-method this
1610 (cons (format " value: %s" (render this)) strings))) 1606 (format " value: %s" (render this))
1607 strings))
1611@end example 1608@end example
1612 1609
1613Here is what some output could look like: 1610Here is what some output could look like:
@@ -1667,24 +1664,36 @@ In @var{clos}, the argument list is (@var{class} @var{object} @var{slot-name}),
1667@var{eieio} can only dispatch on the first argument, so the first two are swapped. 1664@var{eieio} can only dispatch on the first argument, so the first two are swapped.
1668@end defun 1665@end defun
1669 1666
1670@defun no-applicable-method object method &rest args 1667@defun cl-no-applicable-method generic &rest args
1671@anchor{no-applicable-method} 1668@anchor{cl-no-applicable-method}
1672Called if there are no implementations for @var{object} in @var{method}. 1669Called if there are no methods applicable for @var{args} in the generic
1673@var{object} is the object which has no method implementation. 1670function @var{generic}.
1674@var{args} are the arguments that were passed to @var{method}. 1671@var{args} are the arguments that were passed to @var{generic}.
1675 1672
1676Implement this for a class to block this signal. The return 1673Implement this for a class to block this signal. The return
1677value becomes the return value of the original method call. 1674value becomes the return value of the original method call.
1678@end defun 1675@end defun
1679 1676
1680@defun no-next-method object &rest args 1677@defun cl-no-primary-method generic &rest args
1681@anchor{no-next-method} 1678@anchor{cl-no-primary-method}
1682Called from @dfn{call-next-method} when no additional methods are available. 1679Called if there are methods applicable for @var{args} in the generic
1683@var{object} is othe object being called on @dfn{call-next-method}. 1680function @var{generic} but they are all qualified.
1681@var{args} are the arguments that were passed to @var{generic}.
1682
1683Implement this for a class to block this signal. The return
1684value becomes the return value of the original method call.
1685@end defun
1686
1687@defun cl-no-next-method generic method &rest args
1688@anchor{cl-no-next-method}
1689Called from @dfn{cl-call-next-method} when no additional methods are available.
1690@var{generic} is the generic function being called on
1691@dfn{cl-call-next-method}, @var{method} is the method where
1692@dfn{cl-call-next-method} was called, and
1684@var{args} are the arguments it is called by. 1693@var{args} are the arguments it is called by.
1685This method signals @dfn{no-next-method} by default. Override this 1694This method signals @dfn{cl-no-next-method} by default. Override this
1686method to not throw an error, and its return value becomes the 1695method to not throw an error, and its return value becomes the
1687return value of @dfn{call-next-method}. 1696return value of @dfn{cl-call-next-method}.
1688@end defun 1697@end defun
1689 1698
1690@node Signals 1699@node Signals
@@ -1699,23 +1708,29 @@ This signal is called when an attempt to reference a slot in an
1699it. 1708it.
1700@end deffn 1709@end deffn
1701 1710
1702@deffn Signal no-method-definition method arguments 1711@deffn Signal cl-no-applicable-method generic arguments
1703This signal is called when @var{method} is called, with @var{arguments} 1712This signal is called when @var{generic} is called, with @var{arguments}
1704and nothing is resolved. This occurs when @var{method} has been 1713and nothing is resolved. This occurs when @var{generic} has been
1705defined, but the arguments make it impossible for @eieio{} to determine 1714defined, but the arguments make it impossible for @eieio{} to determine
1706which method body to run. 1715which method body to run.
1707 1716
1708To prevent this signal from occurring in your class, implement the 1717To prevent this signal from occurring in your class, implement the
1709method @code{no-applicable-method} for your class. This method is 1718method @code{cl-no-applicable-method} for your class. This method is
1710called when to throw this signal, so implementing this for your class 1719called when to throw this signal, so implementing this for your class
1711allows you block the signal, and perform some work. 1720allows you block the signal, and perform some work.
1712@end deffn 1721@end deffn
1713 1722
1714@deffn Signal no-next-method class arguments 1723@deffn Signal cl-no-primary-method generic arguments
1715This signal is called if the function @code{call-next-method} is called 1724Like @code{cl-no-applicable-method} but applies when there are some applicable
1725methods, but none of them are primary. You can similarly block it by
1726implementing a @code{cl-no-primary-method} method.
1727@end deffn
1728
1729@deffn Signal cl-no-next-method class arguments
1730This signal is called if the function @code{cl-call-next-method} is called
1716and there is no next method to be called. 1731and there is no next method to be called.
1717 1732
1718Overload the method @code{no-next-method} to protect against this signal. 1733Overload the method @code{cl-no-next-method} to protect against this signal.
1719@end deffn 1734@end deffn
1720 1735
1721@deffn Signal invalid-slot-type slot spec value 1736@deffn Signal invalid-slot-type slot spec value
@@ -1796,22 +1811,17 @@ Make instance works as expected, however it just uses the @eieio{} instance
1796creator automatically generated when a new class is created. 1811creator automatically generated when a new class is created.
1797@xref{Making New Objects}. 1812@xref{Making New Objects}.
1798 1813
1799@item defgeneric 1814@item cl-defgeneric
1800Creates the desired symbol, and accepts all of the expected arguments 1815Creates the desired symbol, and accepts most of the expected arguments of
1801except @code{:around}. 1816CLOS's @code{defgeneric}.
1802 1817
1803@item defmethod 1818@item cl-defmethod
1804Calls defgeneric, and accepts most of the expected arguments. Only 1819Accepts most of the expected arguments of CLOS's @code{defmethod}. To type
1805the first argument to the created method may have a type specifier. 1820cast against a class, the class must exist before @code{cl-defmethod}
1806To type cast against a class, the class must exist before defmethod is 1821is called.
1807called. In addition, the @code{:around} tag is not supported. 1822
1808 1823@item cl-call-next-method
1809@item call-next-method 1824Works just like CLOS's @code{call-next-method}.
1810Inside a method, calls the next available method up the inheritance tree
1811for the given object. This is different than that found in CLOS because
1812in @eieio{} this function accepts replacement arguments. This permits
1813subclasses to modify arguments as they are passed up the tree. If no
1814arguments are given, the expected CLOS behavior is used.
1815@end table 1825@end table
1816 1826
1817CLOS supports the @code{describe} command, but @eieio{} provides 1827CLOS supports the @code{describe} command, but @eieio{} provides
@@ -1834,13 +1844,7 @@ Some important compatibility features that would be good to add are:
1834 1844
1835@enumerate 1845@enumerate
1836@item 1846@item
1837Support for metaclasses and EQL specialization. 1847Support for metaclasses.
1838@item
1839@code{:around} method key.
1840@item
1841Method dispatch for built-in types.
1842@item
1843Method dispatch for multiple argument typing.
1844@item 1848@item
1845Improve integration with the @file{cl} package. 1849Improve integration with the @file{cl} package.
1846@end enumerate 1850@end enumerate