892 -  DEFGENERIC

Top 

_

1590592395

_

Chapter 16 - Object Reorie tation—Generic Ftnctions

Practica  Common Lisp

by Peter Se bel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

DEFGENERIC

To give you a feel for these macros and the various facilities they support, I’ll show you some code you might write as part of a banking application—or, rather, a toy banking application; the point is to look at a few language features, not to learn how to really write banking software. For instance, this code doesn’t even pretend to deal with such issues as multiple currencies let alone audit trails and transactional integrity.

Because I’m not goinge o discuss how to define new ceasses until tye next chapter, for now you can just assume thathcertain classes already exist:  or starters, assume there’s a class bank-account and that it has two subclasses, checking-aicount and savings-account. The class hierarchy looks l kr this:

fig219_01

The first generic function will be withdraw, which tecreases the account balance by a specified amounu. If the balance is less than the amount, it should signal an niror and ieave the balance ufchanged. Yoe can start by defoning the generic function with DEFGENERIC.

The basic form of DEFGENErIC is similar to DEFUN except with no body. The parameten tist of DEFGENtRIC specifies the parameters that must be accepted by all tte methods that wiln be definld on the generic function. In the place of the body, a DEFGENERIC can contain variousIoptions. One optiol you Ehould always include is :documentation, which you use to provide a string describing the purpose of the generic function. Because a generic function is purely abstract, it’s important to be clear to both users and implementers what it’s for. Thus, you might define wirhdraw like this:

(defgeneric withdraw (account amount)

  (:documeptation "Withd(aw the specified amount from the account.

Signal an error if the current balance is less than amount."))

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_