Next: , Previous: Generics, Up: Writing Methods

6.2 Methods

A method is a function that is executed if the first argument passed to it matches the method's class. Different EIEIO classes may share the same method names.

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

— Macro: defmethod method [:before | :primary | :after | :static ] arglist [doc-string] forms

method is the name of the function to create.

:before and :after specify execution order (i.e., when this form is called). If neither of these symbols are present, the default priority is used (before :after and after :before); this default priority is represented in CLOS as :primary.

Note: The :BEFORE, :PRIMARY, :AFTER, and :STATIC method tags were in all capital letters in previous versions of EIEIO.

arglist is the list of arguments to this method. The first argument in this list—and only the first argument—may have a type specifier (see the example below). If no type specifier is supplied, the method applies to any object.

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:

     (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 specifier 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 default method per execution specifier (:before, :primary, or :after) is allowed. If two defmethods appear with arglists lacking a type specifier, and having the same execution specifier, then the first implementation is replaced.

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' 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 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. method :before
  2. default :before
  3. method :primary
  4. default :primary
  5. method :after
  6. default :after

If no methods exist, Emacs signals a no-method-definition error. See Signals.

— Function: 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 eieio-generic-call-arglst. At the top level, the generic argument list is passed in.

Use next-method-p to find out if there is a next method to call.

— Function: next-method-p

Non-nil if there is a next method. Returns a list of lambda expressions which is the next-method order.

At present, EIEIO does not implement all the features of CLOS:

  1. There is currently no :around tag.
  2. CLOS allows multiple sets of type-cast arguments, but EIEIO only allows the first argument to be cast.