Next: , Previous: , Up: EIEIO   [Contents][Index]

6 Writing Methods

Writing a method in EIEIO is similar to writing a function. The differences are that there are some extra options and there can be multiple definitions under the same function symbol.

You do it using Emacs Lisp’s built-in support for CLOS-style generic functions via the cl-defgeneric and cl-defmethod macros (see Generic Functions in GNU Emacs Lisp Reference Manual).

EIEIO provides one extension to cl-defmethod to allow methods to dispatch on a class argument: so-called “static” methods do not depend on an object instance, but instead operate on a class. You can create a static method by using the subclass specializer with cl-defmethod:

(cl-defmethod make-instance ((class (subclass mychild)) &rest args)
  (let ((new (cl-call-next-method)))
    (push new all-my-children)
    new))

The argument of a static method will be a class rather than an object. Use the functions oref-default or oset-default which will work on a class.

A class’s make-instance method is defined as a static method.

Note: The subclass specializer is unique to EIEIO.