aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2016-01-22 23:06:22 +0200
committerEli Zaretskii2016-01-22 23:06:22 +0200
commit849a314c7a8a179052b524dfb56c8e723c8f6e82 (patch)
tree1cb959b4ef1a2efe6acbc06ed956f09289ca07c0
parent7c3d742c357dd6480e813f067435b324dba2b325 (diff)
downloademacs-849a314c7a8a179052b524dfb56c8e723c8f6e82.tar.gz
emacs-849a314c7a8a179052b524dfb56c8e723c8f6e82.zip
Document cl-generic.el
* doc/lispref/functions.texi (Generic Functions): New section. (Bug#22336) (Functions): Update the chapter menu. * doc/lispref/elisp.texi: Update the master menu.
-rw-r--r--doc/lispref/elisp.texi1
-rw-r--r--doc/lispref/functions.texi225
-rw-r--r--etc/NEWS4
3 files changed, 230 insertions, 0 deletions
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index da519f579c9..4c1541e98c6 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -536,6 +536,7 @@ Functions
536* Calling Functions:: How to use an existing function. 536* Calling Functions:: How to use an existing function.
537* Mapping Functions:: Applying a function to each element of a list, etc. 537* Mapping Functions:: Applying a function to each element of a list, etc.
538* Anonymous Functions:: Lambda expressions are functions with no names. 538* Anonymous Functions:: Lambda expressions are functions with no names.
539* Generic Functions:: Polymorphism, Emacs-style.
539* Function Cells:: Accessing or setting the function definition 540* Function Cells:: Accessing or setting the function definition
540 of a symbol. 541 of a symbol.
541* Closures:: Functions that enclose a lexical environment. 542* Closures:: Functions that enclose a lexical environment.
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 1e8e7540395..c5f5b4c22c4 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -18,6 +18,7 @@ define them.
18* Calling Functions:: How to use an existing function. 18* Calling Functions:: How to use an existing function.
19* Mapping Functions:: Applying a function to each element of a list, etc. 19* Mapping Functions:: Applying a function to each element of a list, etc.
20* Anonymous Functions:: Lambda expressions are functions with no names. 20* Anonymous Functions:: Lambda expressions are functions with no names.
21* Generic Functions:: Polymorphism, Emacs-style.
21* Function Cells:: Accessing or setting the function definition 22* Function Cells:: Accessing or setting the function definition
22 of a symbol. 23 of a symbol.
23* Closures:: Functions that enclose a lexical environment. 24* Closures:: Functions that enclose a lexical environment.
@@ -1092,6 +1093,230 @@ the compiled code. The byte-compiler cannot assume this list is a
1092function, even though it looks like one, since it does not know that 1093function, even though it looks like one, since it does not know that
1093@code{change-property} intends to use it as a function. 1094@code{change-property} intends to use it as a function.
1094 1095
1096@node Generic Functions
1097@section Generic Functions
1098@cindex generic functions
1099@cindex polymorphism
1100
1101 Functions defined using @code{defun} have a hard-coded set of
1102assumptions about the types and expected values of their arguments.
1103For example, a function that was designed to handle values of its
1104argument that are either numbers or lists of numbers will fail or
1105signal an error if called with a value of any other type, such as a
1106vector or a string. This happens because the implementation of the
1107function is not prepared to deal with types other than those assumed
1108during the design.
1109
1110 By contrast, object-oriented programs use @dfn{polymorphic
1111functions}: a set of specialized functions having the same name, each
1112one of which was written for a certain specific set of argument types.
1113Which of the functions is actually called is decided at run time based
1114on the types of the actual arguments.
1115
1116@cindex CLOS
1117 Emacs provides support for polymorphism. Like other Lisp
1118environments, notably Common Lisp and its Common Lisp Object System
1119(@acronym{CLOS}), this support is based on @dfn{generic functions}.
1120The Emacs generic functions closely follow @acronym{CLOS}, including
1121use of similar names, so if you have experience with @acronym{CLOS},
1122the rest of this section will sound very familiar.
1123
1124 A generic function specifies an abstract operation, by defining its
1125name and list of arguments, but (usually) no implementation. The
1126actual implementation for several specific classes of arguments is
1127provided by @dfn{methods}, which should be defined separately. Each
1128method that implements a generic function has the same name as the
1129generic function, but the method's definition indicates what kinds of
1130arguments it can handle by @dfn{specializing} the arguments defined by
1131the generic function. These @dfn{argument specializers} can be more
1132or less specific; for example, a @code{string} type is more specific
1133than a more general type, such as @code{sequence}.
1134
1135 Note that, unlike in message-based OO languages, such as C@t{++} and
1136Simula, methods that implement generic functions don't belong to a
1137class, they belong to the generic function they implement.
1138
1139 When a generic function is invoked, it selects the applicable
1140methods by comparing the actual arguments passed by the caller with
1141the argument specializers of each method. A method is applicable if
1142the actual arguments of the call are compatible with the method's
1143specializers. If more than one method is applicable, they are
1144combined using certain rules, described below, and the combination
1145then handles the call.
1146
1147@defmac cl-defgeneric name arguments [documentation] [options-and-methods@dots{}] &rest body
1148This macro defines a generic function with the specified @var{name}
1149and @var{arguments}. If @var{body} is present, it provides the
1150default implementation. If @var{documentation} is present (it should
1151always be), it specifies the documentation string for the generic
1152function, in the form @code{(:documentation @var{docstring})}. The
1153optional @var{options-and-methods} can be one of the following forms:
1154
1155@table @code
1156@item (declare @var{declarations})
1157A declare form, as described in @ref{Declare Form}.
1158@item (:argument-precedence-order &rest @var{args})
1159This form affects the sorting order for combining applicable methods.
1160Normally, when two methods are compared during combination, method
1161arguments are examined left to right, and the first method whose
1162argument specializer is more specific will come before the other one.
1163The order defined by this form overrides that, and the arguments are
1164examined according to their order in this form, and not left to right.
1165@item (:method [@var{qualifiers}@dots{}] args &rest body)
1166This form defines a method like @code{cl-defmethod} does.
1167@end table
1168@end defmac
1169
1170@defmac cl-defmethod name [qualifier] arguments &rest [docstring] body
1171This macro defines a particular implementation for the generic
1172function called @var{name}. The implementation code is given by
1173@var{body}. If present, @var{docstring} is the documentation string
1174for the method. The @var{arguments} list, which must be identical in
1175all the methods that implement a generic function, and must match the
1176argument list of that function, provides argument specializers of the
1177form @code{(@var{arg} @var{spec})}, where @var{arg} is the argument
1178name as specified in the @code{cl-defgeneric} call, and @var{spec} is
1179one of the following specializer forms:
1180
1181@table @code
1182@item @var{type}
1183This specializer requires the argument to be of the given @var{type},
1184one of the types from the type hierarchy described below.
1185@item (eql @var{object})
1186This specializer requires the argument be @code{eql} to the given
1187@var{object}.
1188@item (head @var{object})
1189The argument must be a cons cell whose @code{car} is @code{eql} to
1190@var{object}.
1191@item @var{struct-tag}
1192The argument must be an instance of a class named @var{struct-tag}
1193defined with @code{cl-defstruct} (@pxref{Structures,,, cl, Common Lisp
1194Extensions for GNU Emacs Lisp}), or of one of its parent classes.
1195@end table
1196
1197Alternatively, the argument specializer can be of the form
1198@code{&context (@var{expr} @var{spec})}, in which case the value of
1199@var{expr} must be compatible with the specializer provided by
1200@var{spec}; @var{spec} can be any of the forms described above. In
1201other words, this form of specializer uses the value of @var{expr}
1202instead of arguments for the decision whether the method is
1203applicable. For example, @code{&context (overwrite-mode (eql t))}
1204will make the method compatible only when @code{overwrite-mode} is
1205turned on.
1206
1207The type specializer, @code{(@var{arg} @var{type})}, can specify one
1208of the @dfn{system types} in the following list. When a parent type
1209is specified, an argument whose type is any of its more specific child
1210types, as well as grand-children, grand-grand-children, etc. will also
1211be compatible.
1212
1213@table @code
1214@item integer
1215Parent type: @code{number}.
1216@item number
1217@item null
1218Parent type: @code{symbol}
1219@item symbol
1220@item string
1221Parent type: @code{array}.
1222@item array
1223Parent type: @code{sequence}.
1224@item cons
1225Parent type: @code{list}.
1226@item list
1227Parent type: @code{sequence}.
1228@item marker
1229@item overlay
1230@item float
1231Parent type: @code{number}.
1232@item window-configuration
1233@item process
1234@item window
1235@item subr
1236@item compiled-function
1237@item buffer
1238@item char-table
1239Parent type: @code{array}.
1240@item bool-vector
1241Parent type: @code{array}.
1242@item vector
1243Parent type: @code{array}.
1244@item frame
1245@item hash-table
1246@item font-spec
1247@item font-entity
1248@item font-object
1249@end table
1250
1251The optional @var{qualifier} allows to combine several applicable
1252methods. If it is not present, the defined method is a @dfn{primary}
1253method, responsible for providing the primary implementation of the
1254generic function for the specialized arguments. You can also define
1255@dfn{auxiliary methods}, by using one of the following values as
1256@var{qualifier}:
1257
1258@table @code
1259@item :before
1260This auxiliary method will run before the primary method. More
1261accurately, all the @code{:before} methods will run before the
1262primary, in the most-specific-first order.
1263@item :after
1264This auxiliary method will run after the primary method. More
1265accurately, all such methods will run after the primary, in the
1266most-specific-last order.
1267@item :around
1268This auxiliary method will run @emph{instead} of the primary method.
1269The most specific of such methods will be run before any other method.
1270Such methods normally use @code{cl-call-next-method}, described below,
1271to invoke the other auxiliary or primary methods.
1272@item :extra @var{string}
1273This allows to add more methods, distinguished by @var{string}, for
1274the same specializers and qualifiers.
1275@end table
1276@end defmac
1277
1278@cindex dispatch of methods for generic function
1279@cindex multiple-dispatch methods
1280Each time a generic function is called, it builds the @dfn{effective
1281method} which will handle this invocation by combining the applicable
1282methods defined for the function. The process of finding the
1283applicable methods and producing the effective method is called
1284@dfn{dispatch}. The applicable methods are those all of whose
1285specializers are compatible with the actual arguments of the call.
1286Since all of the arguments must be compatible with the specializers,
1287they all determine whether a method is applicable. Methods that
1288explicitly specialize more than one argument are called
1289@dfn{multiple-dispatch methods}.
1290
1291The applicable methods are sorted into the order in which they will be
1292combined. The method whose left-most argument specializer is the most
1293specific one will come first in the order. (Specifying
1294@code{:argument-precedence-order} as part of @code{cl-defmethod}
1295overrides that, as described above.) If the method body calls
1296@code{cl-call-next-method}, the next most-specific method will run.
1297If there are applicable @code{:around} methods, the most-specific of
1298them will run first; it should call @code{cl-call-next-method} to run
1299any of the less specific @code{:around} methods. Next, the
1300@code{:before} methods run in the order of their specificity, followed
1301by the primary method, and lastly the @code{:after} methods in the
1302reverse order of their specificity.
1303
1304@defun cl-call-next-method &rest args
1305When invoked from within the lexical body of a primary or an
1306@code{:around} auxiliary method, call the next applicable method for
1307the same generic function. Normally, it is called with no arguments,
1308which means to call the next applicable method with the same arguments
1309that the calling method was invoked. Otherwise, the specified
1310arguments are used instead.
1311@end defun
1312
1313@defun cl-next-method-p
1314This function, when called from within the lexical body of a primary
1315or an @code{:around} auxiliary method, returns non-@code{nil} if there
1316is a next method to call.
1317@end defun
1318
1319
1095@node Function Cells 1320@node Function Cells
1096@section Accessing Function Cell Contents 1321@section Accessing Function Cell Contents
1097 1322
diff --git a/etc/NEWS b/etc/NEWS
index 4e47c5882f9..03e6148ed8e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -520,6 +520,7 @@ If you need your objects to be named, do it by inheriting from `eieio-named'.
520+++ 520+++
521*** The <initarg> variables are declared obsolete. 521*** The <initarg> variables are declared obsolete.
522*** defgeneric and defmethod are declared obsolete. 522*** defgeneric and defmethod are declared obsolete.
523Use the equivalent facilities from cl-generic.el instead.
523+++ 524+++
524*** `constructor' is now an obsolete alias for `make-instance'. 525*** `constructor' is now an obsolete alias for `make-instance'.
525 526
@@ -1177,7 +1178,10 @@ command is called from Emacs (i.e., INSIDE_EMACS environment variable
1177is set). This feature requires newer versions of GnuPG (2.1.5 or 1178is set). This feature requires newer versions of GnuPG (2.1.5 or
1178later) and Pinentry (0.9.5 or later). 1179later) and Pinentry (0.9.5 or later).
1179 1180
1181+++
1180** cl-generic.el provides CLOS-style multiple-dispatch generic functions. 1182** cl-generic.el provides CLOS-style multiple-dispatch generic functions.
1183The main entry points are `cl-defgeneric' and `cl-defmethod'. See the
1184node "Generic Functions" in the Emacs Lisp manual for more details.
1181 1185
1182--- 1186---
1183** scss-mode (a minor variant of css-mode) 1187** scss-mode (a minor variant of css-mode)