Multiple dispatching provides increased expressive power over single dispatching by guiding method lookup using the values of all arguments instead of only the receiver. However, existing languages with multiple dispatching do not encourage the dataabstraction-oriented programming style that is encouraged by traditional single-dispatching languages; instead existing multiple-dispatching languages tend to foster a functionoriented programming style organized around generic functions. We propose an alternative view of multiple dispatching that is intended to promote a data-abstraction-oriented programming style. Instead of viewing a multi-method as "outside" of all objects, we view a multi-method as "inside" the objects for which the multi-method applies (on which it dispatches). Because objects are closely connected to the multi-methods implementing their operations, the internals of an object can be encapsulated by being accessible only to the closely-connected multi-methods. We are exploring this object-oriented view of multimethods in the context of a new programming language named Cecil. In most object-oriented languages, a message is sent to a distinguished receiver object, and the run-time "type" of the receiver determines the method that is invoked by the message. Other arguments of the message are passed on to the invoked method but do not participate in method lookup. This style of object-oriented language is termed a single-dispatching language, since method lookup (a.k.a. dispatching) is performed only with respect to the single receiver argument. Single dispatching works well for many kinds of messages, especially those in which the first argument is more "interesting" that the others, in the sense that the first argument alone determines what code should be run to carry out the operation. However, for some kinds of messages, several arguments may be "interesting," with no clear reason to prefer one argument over another. For example, for most standard binary arithmetic messages such as +, both arguments are equally interesting, and dispatching on only one is unnatural. If this problem were confined to pre-defined abstractions like numbers and arithmetic, then perhaps it could be overlooked as an irksome complication of arithmetic, solved once and for all by the system implementors and ignored from then on. However, the asymmetry of single dispatching complicates other kinds of messages. For example: • Most data types define an equality testing binary operation, and many kinds of objects define ordering relations. The same issues arise as with the standard arithmetic messages. • The pairDo(c1, c2, block) message takes two collections and a closure block and iterates through the two collections in parallel. Neither collection is more important than the other, yet in single-dispatching languages the programmer must favor one collection over the other when selecting the initial implementation of pairDo.
Cecil REF is the first language known by the authors to integrate a prototype-inspired object model with multiple dispatch.
Object-Oriented Multi-Methods in Cecil
ECOOP
Computer Science