Previous: Chained Methods, Up: Methods


5.4 Computed Methods

A computed method is a powerful mechanism that provides the ability to generate methods “on the fly”. A computed method is like an ordinary method, except that its procedure is called during method combination, and is passed the classes of the arguments in place of the arguments themselves. Based on these classes, the computed method returns an ordinary method, which is combined in the usual way.

Note that computed methods and computed emps both satisfy the predicate method?. They are not really methods in that they cannot be combined with other methods to form an effective method procedure; however, they are treated as methods by procedures such as add-method and method-specializers.

— Procedure: make-computed-method specializers procedure

Create and return a computed method. Procedure will be called during method combination with the classes of the generic-procedure arguments as its arguments. It must return one of the following:

— Procedure: computed-method? object

Returns #t if object is a computed method, otherwise returns #f.

A computed emp takes the computed-method mechanism one step further. A computed emp is like a computed method, except that it returns an effective method procedure rather than a method. compute-effective-method-procedure tries each of the applicable computed emps, and if exactly one of them returns an emp, that is the resulting effective method procedure.

— Procedure: make-computed-emp key specializers procedure

Create and return a computed emp. Procedure will be called during method combination with the classes of the generic-procedure arguments as its arguments. It must return either an emp or #f.

Key is an arbitrary object that is used to identify the computed emp. The key is used by add-method and delete-method to decide whether two computed emps are the same; they are the same if their keys are equal?. This is necessary because a generic procedure may have more than one computed emp with the same specializers.

— Procedure: computed-emp? object

Returns #t if object is a computed emp, otherwise returns #f.

— Generic Procedure: computed-emp-key computed-emp

Returns the key for computed-emp.