6.2 Methods

A method is a function that is executed if the arguments passed to it matches the method’s specializers. Different EIEIO classes may share the same method names.

Methods are created with the cl-defmethod macro, which is similar to defun.

Macro: cl-defmethod method [:before | :around | :after ] arglist [doc-string] forms

method is the name of the function to create.

:before, :around, and :after specify execution order (i.e., when this form is called). If none of these symbols are present, the method is said to be a primary.

arglist is the list of arguments to this method. The mandatory arguments in this list may have a type specializer (see the example below) which means that the method will only apply when those arguments match the given type specializer. An argument with no type specializer means that the method applies regardless of its value.

doc-string is the documentation attached to the implementation. All method doc-strings are incorporated into the generic method’s function documentation.

forms is the body of the function.

In the following example, we create a method mymethod for the classname class:

(cl-defmethod mymethod ((obj classname) secondarg)
  "Doc string" )

This method only executes if the obj argument passed to it is an EIEIO object of class classname.

A method with no type specializer is a default method. If a given class has no implementation, then the default method is called when that method is used on a given object of that class.

Only one method per combination of specializers and qualifiers (:before, :around, or :after) is kept. If two cl-defmethods appear with the same specializers and the same qualifiers, then the second implementation replaces the first.

When a method is called on an object, but there is no method specified for that object, but there is a method specified for object’s parent class, the parent class’s method is called. If there is a method defined for both, only the child’s method is called. A child method may call a parent’s method using cl-call-next-method, described below.

If multiple methods and default methods are defined for the same method and class, they are executed in this order:

  1. :around methods The most specific :around method is called first, which may invoke the less specific ones via cl-call-next-method. If it doesn’t invoke cl-call-next-method, then no other methods will be executed. When there are no more :around methods to call, falls through to run the other (non-:around) methods.
  2. :before methods Called in sequence from most specific to least specific.
  3. primary methods The most specific method is called, which may invoke the less specific ones via cl-call-next-method.
  4. :after methods Called in sequence from least specific to most specific.

If no methods exist, Emacs signals a cl-no-applicable-method error. See Signals. If methods exist but none of them are primary, Emacs signals a cl-no-primary-method error. See Signals.

Function: cl-call-next-method &rest replacement-args

This function calls the superclass method from a subclass method. This is the “next method” specified in the current method list.

If replacement-args is non-nil, then use them instead of the arguments originally provided to the method.

Can only be used from within the lexical body of a primary or around method.

Function: cl-next-method-p

Non-nil if there is a next method.

Can only be used from within the lexical body of a primary or around method.