Next: , Previous: , Up: Programming techniques   [Contents][Index]


4.1 Object-oriented programming

FEMLISP is implemented largely in an object-oriented manner using CLOS (Common Lisp Object System). CLOS is very powerful, featuring, for example, multiple inheritance, multi-argument dispatch, and class-redefinition at runtime. We do not want to go into detail here, but refer to the books (Keene 1989) and (Kiczales et al, 1991). Nevertheless, we want to discuss briefly some specialities of object-oriented programming in Common Lisp which are not common in other programming languages.

Dispatch of a generic function on more than one argument is often useful. One example is the following code for a matrix-vector multiplication, where the method cannot be assigned clearly to either the class <matrix> or the class <vector>.

(defmethod m* ((A <matrix>) (y <vector>))
  (let ((x (make-row-vector-for A)))
    (x+=Ay x A y)
    x))

Method combination opens up a nice way for enhanced code re-use by allowing also :before- and :after-methods which are called before and after the execution of the primary method. For example, the following modifying method adds a compatibility check to the above matrix-vector multiplication which is used for every application of m* (and regardless of the type of the arguments):

(defmethod m* :before (A y)
  (assert (= (ncols A) (nrows y))))

Multiple inheritance is usually not used as much as single inheritance, so that some object-oriented programming languages, for example Java, do not provide it at all. Nevertheless, it can be quite useful. In FEMLISP, it is used, for example, to define so-called mixin classes which are used to dispatch the behaviour of a multigrid scheme between the standard "Correction Scheme" (CS) and Brandt’s "Full Approximation Storage" (FAS).


Next: , Previous: , Up: Programming techniques   [Contents][Index]