Defgeneric


:documentation

:method

:method-combination

:argument-precedence-order

:documentation

Sets up basic generic function (name, number and names of arguments) plus a documentation string.

(defgeneric Area (Geometric-Object)
  (:documentation "Area of geometric object in square meters"))

:method

Lets you define multiple methods in one location. Use defmethod if you want the methods to be together with the class definition (i.e. all stuff related to rectangles in one place), use defgeneric if you want the methods to be together with each other (i.e. all stuff related to area in one place).

(defgeneric Area (Geometric-Object)
  (:documentation "Area of geometric object in square meters")
  (:method ((C Circle)) (* pi (expt (Radius C) 2)))
  (:method ((R Rectangle)) (* (Width R) (Height R)))
  ...)

:method-combination

Allows you to change how inherited values get returned for a given method. The default is to return a single value that is the value of the most specific method. However, you can specify that the values get combined into a list, added together, etc.

Some options:

STANDARD
The default.

PROGN
Execute all methods that apply in most-specific to least-specific order.

(defgeneric Print-Identity (Obj)
  (:method-combination progn))
(defmethod Print-Identity progn ((R Rectangle))
  (print "I'm a Rectangle")
  R)
(defmethod Print-Identity progn ((S Square))
  (print "I'm a Square")
  S)

(Print-Identity Square-1)
"I'm a Square"
"I'm a Rectangle"
==> #<Square-1>
Other options include +, list, and, or, max, min, append and nconc.

:argument-precedence-order

List the args (as specified in the defgeneric parameter list) in the order you want. The default is to consider them in left to right order. For instance, consider:

(defmethod Foo (Item (L list))
  (Frobify Item L))

(defmethod Foo ((L list) Item)
  (Munge Item L))

Now consider a call to Foo with two lists: (Foo '(A B C) '(D E F)). The second version of Foo will get invoked, since it has the more specific match on the first argument, and the arguments will get munged instead of frobified. However, if you had first done:

(defgeneric Foo (Item1 Item2)
  (:documentation "Foo takes 2 entries and Munges or Frobifies them")
  (:argument-precedence-order Item2 Item1))

then (Foo '(A B C) '(D E F)) would have gotten the first version, since it is has a more specific match on Item2, which is listed first in the :argument-precedence-order entry.

Table of contents

:documentation
:method
:method-combination
:argument-precedence-order