17.05.2015 Views

zl:1 - FTP

zl:1 - FTP

zl:1 - FTP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

defgeneric<br />

136<br />

(lambda-macro ilisp (x)<br />

'(lambda (&optional ,@(second x) &rest ignore) . ,(cddr x»)<br />

Then the following example would define a function called new-list that<br />

would use the lambda macro called ilisp:<br />

(deffunction new-list ilisp (x y z)<br />

(list x y z»<br />

new-list's arguments are optional, and any extra arguments are ignored.<br />

Examples:<br />

(new-list 1 2) => (1 2 nil)<br />

(new-list 1 2 3 4) -> (1 2 3)<br />

I<br />

defgeneric generic-function-name (argl arg2 ... ) options... Special Form<br />

Defines a generic function named generic-function-name that accepts arguments<br />

defined by (argl arg2 ... ), a lambda-list. The first argument, argl,<br />

is required, unless the :function option is used to indicate otherwise. argl<br />

represents the object that is supplied as the first argument to the generic<br />

function. The flavor of argl determines which method is appropriate to<br />

perform this generic function on the object. Any additional arguments<br />

(arg2, and so on) are passed to the methods.<br />

The arguments to defgeneric are displayed when you give the Arglist (M-X)<br />

command or press c-sh-A while this generic function is current.<br />

For example, to define a generic function total-fuel-supply that works on<br />

instances of army and navy, and takes one argument (fuel-type) in addition<br />

to the object itself, we might supply military-group as argl:<br />

(defgeneric total-fuel-supply (military-group fuel-type)<br />

"Returns today's total supply<br />

of the given type of fuel<br />

available to the given military group."<br />

(:method-combination :sum»<br />

The generic function is called as follows:<br />

(total-fuel-supply blue-army ':gas)<br />

The argument blue-army is known to be of flavor army. Therefore,<br />

Flavors chooses the method that implements the total-fuel-supply generic<br />

function on instances of the army flavor. That method takes only one argument,<br />

fuel-type:<br />

(defmethod (total-fuel-supply army) (fuel-type)<br />

body of method)<br />

The set of options for defgeneric are described elsewhere: See the section<br />

"Options For defgeneric" in Symbolics Common Lisp: Language Concepts.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!