chickadee » operations » operation

(operation default . method-clauses)syntax

Returns: operation

The syntax of OPERATION is the same as that of OBJECT, but its semantics and application are somewhat different. An OPERATION-expression evaluates to an operation. When called, the operation obtains a handler for its first argument, calls the handler to obtain a method, and then invokes the method. The default method for the operation is established as being default.

As the subject of another generic operation, an operation is an object like any other, and in this case the operation acts just as if it had been created by an OBJECT-expression with the same method-clauses. In this way one can establish the behavior of an operation when subject to other operations, for example SETTER.

The following rough equivalence describes the semantics of OPERATION. Some details have been omitted.

 (operation default . methods)
 (labels ((op (object (lambda (obj . args)
                      (let ((method ((get-handler obj) op)))
                        (cond (method
                               (apply method obj args))
                               (apply default obj args)))))
                    . methods)))

For example:

 (define op (operation (lambda (obj) 'zebu)))
 (op (object #f ((op self) 'quagga)))    -->  quagga
 (op 'eland)                              -->  zebu

An operation is created, and the variable OP is bound to it. The operation's default method always returns the symbol ZEBU. When the operation is applied to the value of the OBJECT-expression, the appropriate method is invoked, and the call to the operation yields the symbol QUAGGA. When the operation is applied to an object which doesn't handle it - the symbol ELAND - the operation's default method is invoked, so the call yields the symbol ZEBU.