Saturday, March 05, 2005

Advanced Generic Function Techniques

Sometimes, more than one method of a PyProtocols generic function (or entry in a Dispatcher object) applies in a given circumstance. For example, you might need to sum the results of a series of pricing rules in order to compute a product's price. Or, sometimes you'd like a method to be able to modify the result of a less-specific method.

For these scenarios, you will want to use one of several "result combination" techniques, ranging from using a provided subclass of GenericFunction or Dispatcher, to rolling your own entirely custom combination approach.

See the full article at Generic Function Result Combination in the PEAK DevCenter Wiki.

(P.S. for Lispers: the article also documents PyProtocols' CLOS-style method combining, including "call next method" and "before/after/around" methods, and shows you how to create custom qualifiers as in CLOS. However, unlike CLOS, PyProtocols uses unambiguous logical implication for ordering rather than CLOS's lexical ordering and asymmetric argument dispatch techniques. That is, in the presence of ambiguity, it forces the programmer to say what they mean instead of using semi-arbitrary precedence rules.)