diff options
| author | Eli Zaretskii | 2016-01-22 23:06:22 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2016-01-22 23:06:22 +0200 |
| commit | 849a314c7a8a179052b524dfb56c8e723c8f6e82 (patch) | |
| tree | 1cb959b4ef1a2efe6acbc06ed956f09289ca07c0 | |
| parent | 7c3d742c357dd6480e813f067435b324dba2b325 (diff) | |
| download | emacs-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.texi | 1 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 225 | ||||
| -rw-r--r-- | etc/NEWS | 4 |
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 | |||
| 1092 | function, even though it looks like one, since it does not know that | 1093 | function, 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 | ||
| 1102 | assumptions about the types and expected values of their arguments. | ||
| 1103 | For example, a function that was designed to handle values of its | ||
| 1104 | argument that are either numbers or lists of numbers will fail or | ||
| 1105 | signal an error if called with a value of any other type, such as a | ||
| 1106 | vector or a string. This happens because the implementation of the | ||
| 1107 | function is not prepared to deal with types other than those assumed | ||
| 1108 | during the design. | ||
| 1109 | |||
| 1110 | By contrast, object-oriented programs use @dfn{polymorphic | ||
| 1111 | functions}: a set of specialized functions having the same name, each | ||
| 1112 | one of which was written for a certain specific set of argument types. | ||
| 1113 | Which of the functions is actually called is decided at run time based | ||
| 1114 | on the types of the actual arguments. | ||
| 1115 | |||
| 1116 | @cindex CLOS | ||
| 1117 | Emacs provides support for polymorphism. Like other Lisp | ||
| 1118 | environments, notably Common Lisp and its Common Lisp Object System | ||
| 1119 | (@acronym{CLOS}), this support is based on @dfn{generic functions}. | ||
| 1120 | The Emacs generic functions closely follow @acronym{CLOS}, including | ||
| 1121 | use of similar names, so if you have experience with @acronym{CLOS}, | ||
| 1122 | the rest of this section will sound very familiar. | ||
| 1123 | |||
| 1124 | A generic function specifies an abstract operation, by defining its | ||
| 1125 | name and list of arguments, but (usually) no implementation. The | ||
| 1126 | actual implementation for several specific classes of arguments is | ||
| 1127 | provided by @dfn{methods}, which should be defined separately. Each | ||
| 1128 | method that implements a generic function has the same name as the | ||
| 1129 | generic function, but the method's definition indicates what kinds of | ||
| 1130 | arguments it can handle by @dfn{specializing} the arguments defined by | ||
| 1131 | the generic function. These @dfn{argument specializers} can be more | ||
| 1132 | or less specific; for example, a @code{string} type is more specific | ||
| 1133 | than a more general type, such as @code{sequence}. | ||
| 1134 | |||
| 1135 | Note that, unlike in message-based OO languages, such as C@t{++} and | ||
| 1136 | Simula, methods that implement generic functions don't belong to a | ||
| 1137 | class, they belong to the generic function they implement. | ||
| 1138 | |||
| 1139 | When a generic function is invoked, it selects the applicable | ||
| 1140 | methods by comparing the actual arguments passed by the caller with | ||
| 1141 | the argument specializers of each method. A method is applicable if | ||
| 1142 | the actual arguments of the call are compatible with the method's | ||
| 1143 | specializers. If more than one method is applicable, they are | ||
| 1144 | combined using certain rules, described below, and the combination | ||
| 1145 | then handles the call. | ||
| 1146 | |||
| 1147 | @defmac cl-defgeneric name arguments [documentation] [options-and-methods@dots{}] &rest body | ||
| 1148 | This macro defines a generic function with the specified @var{name} | ||
| 1149 | and @var{arguments}. If @var{body} is present, it provides the | ||
| 1150 | default implementation. If @var{documentation} is present (it should | ||
| 1151 | always be), it specifies the documentation string for the generic | ||
| 1152 | function, in the form @code{(:documentation @var{docstring})}. The | ||
| 1153 | optional @var{options-and-methods} can be one of the following forms: | ||
| 1154 | |||
| 1155 | @table @code | ||
| 1156 | @item (declare @var{declarations}) | ||
| 1157 | A declare form, as described in @ref{Declare Form}. | ||
| 1158 | @item (:argument-precedence-order &rest @var{args}) | ||
| 1159 | This form affects the sorting order for combining applicable methods. | ||
| 1160 | Normally, when two methods are compared during combination, method | ||
| 1161 | arguments are examined left to right, and the first method whose | ||
| 1162 | argument specializer is more specific will come before the other one. | ||
| 1163 | The order defined by this form overrides that, and the arguments are | ||
| 1164 | examined according to their order in this form, and not left to right. | ||
| 1165 | @item (:method [@var{qualifiers}@dots{}] args &rest body) | ||
| 1166 | This 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 | ||
| 1171 | This macro defines a particular implementation for the generic | ||
| 1172 | function called @var{name}. The implementation code is given by | ||
| 1173 | @var{body}. If present, @var{docstring} is the documentation string | ||
| 1174 | for the method. The @var{arguments} list, which must be identical in | ||
| 1175 | all the methods that implement a generic function, and must match the | ||
| 1176 | argument list of that function, provides argument specializers of the | ||
| 1177 | form @code{(@var{arg} @var{spec})}, where @var{arg} is the argument | ||
| 1178 | name as specified in the @code{cl-defgeneric} call, and @var{spec} is | ||
| 1179 | one of the following specializer forms: | ||
| 1180 | |||
| 1181 | @table @code | ||
| 1182 | @item @var{type} | ||
| 1183 | This specializer requires the argument to be of the given @var{type}, | ||
| 1184 | one of the types from the type hierarchy described below. | ||
| 1185 | @item (eql @var{object}) | ||
| 1186 | This specializer requires the argument be @code{eql} to the given | ||
| 1187 | @var{object}. | ||
| 1188 | @item (head @var{object}) | ||
| 1189 | The argument must be a cons cell whose @code{car} is @code{eql} to | ||
| 1190 | @var{object}. | ||
| 1191 | @item @var{struct-tag} | ||
| 1192 | The argument must be an instance of a class named @var{struct-tag} | ||
| 1193 | defined with @code{cl-defstruct} (@pxref{Structures,,, cl, Common Lisp | ||
| 1194 | Extensions for GNU Emacs Lisp}), or of one of its parent classes. | ||
| 1195 | @end table | ||
| 1196 | |||
| 1197 | Alternatively, 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 | ||
| 1201 | other words, this form of specializer uses the value of @var{expr} | ||
| 1202 | instead of arguments for the decision whether the method is | ||
| 1203 | applicable. For example, @code{&context (overwrite-mode (eql t))} | ||
| 1204 | will make the method compatible only when @code{overwrite-mode} is | ||
| 1205 | turned on. | ||
| 1206 | |||
| 1207 | The type specializer, @code{(@var{arg} @var{type})}, can specify one | ||
| 1208 | of the @dfn{system types} in the following list. When a parent type | ||
| 1209 | is specified, an argument whose type is any of its more specific child | ||
| 1210 | types, as well as grand-children, grand-grand-children, etc. will also | ||
| 1211 | be compatible. | ||
| 1212 | |||
| 1213 | @table @code | ||
| 1214 | @item integer | ||
| 1215 | Parent type: @code{number}. | ||
| 1216 | @item number | ||
| 1217 | @item null | ||
| 1218 | Parent type: @code{symbol} | ||
| 1219 | @item symbol | ||
| 1220 | @item string | ||
| 1221 | Parent type: @code{array}. | ||
| 1222 | @item array | ||
| 1223 | Parent type: @code{sequence}. | ||
| 1224 | @item cons | ||
| 1225 | Parent type: @code{list}. | ||
| 1226 | @item list | ||
| 1227 | Parent type: @code{sequence}. | ||
| 1228 | @item marker | ||
| 1229 | @item overlay | ||
| 1230 | @item float | ||
| 1231 | Parent 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 | ||
| 1239 | Parent type: @code{array}. | ||
| 1240 | @item bool-vector | ||
| 1241 | Parent type: @code{array}. | ||
| 1242 | @item vector | ||
| 1243 | Parent 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 | |||
| 1251 | The optional @var{qualifier} allows to combine several applicable | ||
| 1252 | methods. If it is not present, the defined method is a @dfn{primary} | ||
| 1253 | method, responsible for providing the primary implementation of the | ||
| 1254 | generic 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 | ||
| 1260 | This auxiliary method will run before the primary method. More | ||
| 1261 | accurately, all the @code{:before} methods will run before the | ||
| 1262 | primary, in the most-specific-first order. | ||
| 1263 | @item :after | ||
| 1264 | This auxiliary method will run after the primary method. More | ||
| 1265 | accurately, all such methods will run after the primary, in the | ||
| 1266 | most-specific-last order. | ||
| 1267 | @item :around | ||
| 1268 | This auxiliary method will run @emph{instead} of the primary method. | ||
| 1269 | The most specific of such methods will be run before any other method. | ||
| 1270 | Such methods normally use @code{cl-call-next-method}, described below, | ||
| 1271 | to invoke the other auxiliary or primary methods. | ||
| 1272 | @item :extra @var{string} | ||
| 1273 | This allows to add more methods, distinguished by @var{string}, for | ||
| 1274 | the same specializers and qualifiers. | ||
| 1275 | @end table | ||
| 1276 | @end defmac | ||
| 1277 | |||
| 1278 | @cindex dispatch of methods for generic function | ||
| 1279 | @cindex multiple-dispatch methods | ||
| 1280 | Each time a generic function is called, it builds the @dfn{effective | ||
| 1281 | method} which will handle this invocation by combining the applicable | ||
| 1282 | methods defined for the function. The process of finding the | ||
| 1283 | applicable methods and producing the effective method is called | ||
| 1284 | @dfn{dispatch}. The applicable methods are those all of whose | ||
| 1285 | specializers are compatible with the actual arguments of the call. | ||
| 1286 | Since all of the arguments must be compatible with the specializers, | ||
| 1287 | they all determine whether a method is applicable. Methods that | ||
| 1288 | explicitly specialize more than one argument are called | ||
| 1289 | @dfn{multiple-dispatch methods}. | ||
| 1290 | |||
| 1291 | The applicable methods are sorted into the order in which they will be | ||
| 1292 | combined. The method whose left-most argument specializer is the most | ||
| 1293 | specific one will come first in the order. (Specifying | ||
| 1294 | @code{:argument-precedence-order} as part of @code{cl-defmethod} | ||
| 1295 | overrides that, as described above.) If the method body calls | ||
| 1296 | @code{cl-call-next-method}, the next most-specific method will run. | ||
| 1297 | If there are applicable @code{:around} methods, the most-specific of | ||
| 1298 | them will run first; it should call @code{cl-call-next-method} to run | ||
| 1299 | any of the less specific @code{:around} methods. Next, the | ||
| 1300 | @code{:before} methods run in the order of their specificity, followed | ||
| 1301 | by the primary method, and lastly the @code{:after} methods in the | ||
| 1302 | reverse order of their specificity. | ||
| 1303 | |||
| 1304 | @defun cl-call-next-method &rest args | ||
| 1305 | When invoked from within the lexical body of a primary or an | ||
| 1306 | @code{:around} auxiliary method, call the next applicable method for | ||
| 1307 | the same generic function. Normally, it is called with no arguments, | ||
| 1308 | which means to call the next applicable method with the same arguments | ||
| 1309 | that the calling method was invoked. Otherwise, the specified | ||
| 1310 | arguments are used instead. | ||
| 1311 | @end defun | ||
| 1312 | |||
| 1313 | @defun cl-next-method-p | ||
| 1314 | This function, when called from within the lexical body of a primary | ||
| 1315 | or an @code{:around} auxiliary method, returns non-@code{nil} if there | ||
| 1316 | is 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 | ||
| @@ -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. |
| 523 | Use 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 | |||
| 1177 | is set). This feature requires newer versions of GnuPG (2.1.5 or | 1178 | is set). This feature requires newer versions of GnuPG (2.1.5 or |
| 1178 | later) and Pinentry (0.9.5 or later). | 1179 | later) 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. |
| 1183 | The main entry points are `cl-defgeneric' and `cl-defmethod'. See the | ||
| 1184 | node "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) |