Node:Basic Generic Function Creation, Next:, Up:Creating Generic Functions



3.5.1 Basic Generic Function Creation

The following forms may be used to bind a variable to a generic function. Depending on that variable's pre-existing value, the generic function may be created empty - with no methods - or it may contain methods that are inferred from the pre-existing value.

It is not, in general, necessary to use define-generic or define-accessor before defining methods for the generic function using define-method, since define-method will automatically interpolate a define-generic call, or upgrade an existing generic to an accessor, if that is implied by the define-method call. Note in particular that, if the specified variable already has a generic function value, define-generic and define-accessor will discard it! Obviously it is application-dependent whether this is desirable or not.

If, for example, you wanted to extend + for a class representing a new numerical type, you probably want to inherit any existing methods for + and so should not use define-generic. If, on the other hand, you do not want to risk inheriting methods whose behaviour might surprise you, you can use define-generic or define-accessor to wipe the slate clean.

define-generic symbol syntax
Create a generic function with name symbol and bind it to the variable symbol.

If the variable symbol was previously bound to a Scheme procedure (or procedure-with-setter), the old procedure (and setter) is incorporated into the new generic function as its default procedure (and setter). Any other previous value that was bound to symbol, including an existing generic function, is overwritten by the new generic function.

define-accessor symbol syntax
Create an accessor with name symbol and bind it to the variable symbol.

If the variable symbol was previously bound to a Scheme procedure (or procedure-with-setter), the old procedure (and setter) is incorporated into the new accessor as its default procedure (and setter). Any other previous value that was bound to symbol, including an existing generic function or accessor, is overwritten by the new definition.