[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7. Writing Methods

Writing a CLOS style method is similar to writing a function. The differences are that there are some extra options and there can be multiple implementations of a single method which interact interestingly with each other.

Each method created verifies that there is a generic method available to attach to. A generic method has no body, and is merely a symbol upon which methods are attached.

Function: defgeneric method arglist [doc-string]

method is the unquoted symbol to turn into a function. arglist is the default list of arguments to use (not implemented yet). doc-string is the documentation used for this symbol.

A generic function acts as a place holder for methods. There is no need to call defgeneric yourself, as defmethod will call it if necessary. Currently the argument list is unused.

defgeneric will prevent you from turning an existing emacs lisp function into a generic function.

Function: defmethod method [:BEFORE | :PRIMARY | :AFTER | :STATIC ] arglist [doc-string] forms

method is the name of the function to be created.

:BEFORE | :AFTER represent when this form is to be called. If neither of these symbols are present, then the default priority is, before :AFTER, after :BEFORE, and is represented in CLOS as PRIMARY.

If :STATIC is used, then the first argument when calling this function can be a class or an object. Never treat the first argument of a STATIC method as an object, always used oref-default or oset-default. A Class' construction is defined as a static method.

arglist is the argument list. Unlike CLOS, only the FIRST argument may be type-cast, and it may only be type-cast to an EIEIO object. An arglist such as (a b) would classify the function as generic call, which has no object it can talk to (none is passed in) and merely allows the creation of side-effects. If the arglist appears as ((this data-object) b) then the form is stored as belonging to the class data-object.

The first argument does not need to be typecast. A method with no typecast is a generic. If a given class has no implementation, then the generic will be called when that method is used on a given object of that class.

If two defmethods appear with arglists such as (a b) and (c d) then one of the implementations will be overwritten, but generic and multiple type cast arglists can co-exist.

When called, if there is a method cast against the object's parent class, but not for that object's class, the parent class' method will be called. If there is a method defined for both, only the child's method is called.

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

forms is the body of the function.

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

method :BEFORE
generic :BEFORE
method :PRIMARY
generic :PRIMARY
method :AFTER
generic :AFTER

If in any situation a method does not exist, but a generic does, then the generic is called in place of the method.

If no methods exist, then the signal no-method-definition is thrown. 11. Signals

See the file `eieio-test.el' for an example testing these differently tagged methods.

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

While running inside a CLOS method, calling this function will call the method associated with the parent of the class of the currently running method with the same parameters.

If no next method is available, but a generic is implemented for the given key (Such as :BEFORE), then the generic will be called.

OPTIONAL arguments replacement-args can be used to replace the arguments the next method would be called with. Useful if a child class wishes to add additional behaviors through the modification of the parameters. This is not a feature of CLOS.

For example code See section 4. Default Superclass.

Function: call-next-method-p

Return t if there is a next method we can call.

In this implementation, not all features of CLOS exist.

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

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html