Multiple and predicative dispatch
| Speaker | David Mertz |
|---|---|
| Track | Data Science and Engineering |
| Type | Regular talk (45 minutes) |
Abstract
One of the fundamental operations in structured programming is "dispatching" operations to relevant blocks of code, based on some criteria a program can determine. Most Python programmers are familiar with object-oriented programming in which a call to instance.method() will examine the class of instance to determine the specific code called. This uses something called an MRO (method resolution order). However, OOP is far from the only possible dispatch strategy.
One alternative approach is something called multiple dispatch, also known as multimethods. Under this approach, the types (and perhaps number) of the arguments to a function rather than its inheritance tree determine which code is utilized to implement a function or method call. Function overloading in languages like C++, Java, Julia, or Fortran, are limited examples of multiple dispatch; Python itself also offers a limited version with its @singledispatch operator.
Under true multiple dispatch, the types of ALL the arguments to a function are considered in the decision of which code to execute. Many people have implemented Python modules to support full multiple dispatch, dating from the early 2000s, including for example, Guido van Rossum and myself. Those are all nice tools, varying somewhat in syntactic approaches and specific resolution rules.
An extension of this idea has been implemented less widely, but I have created an example implementation which I believe has some nice properties. This idea is "predicative dispatch." We can create several versions of a function that vary not only in the data types passed as arguments, but also by properties these arguments might have. In a simple example, we might have code paths for integer arguments, but also different paths depending on whether these integers are positive or negative.
