Dispatching For Dummies

OK, so I don't actually think I'm a dummy, but some of the pages here certainly make me feel like one. I have an eclectic background, but I've never come across "dispatching" explained in a way I can really understand.

After the recent success with coming to grips with RSS, I thought I'd ask here.

Just what is "dispatching"?

Simple, really. The selection of a method/function/what-have-you, based on its arguments, allowing calls which have different runtime behavior to share syntax.

From reading some of the pages it sounds like messages are being "dispatched" to different objects, but people seem to be talking about implementation details and dark corners - it's hard to find a simple description. I thought messages were simply addressed to and delivered to methods, but there's obviously something I'm not getting.

Any takers? For reference I have started to read, but not generally understood:

Table Oriented Programming (controversial, I know, but at its essence it is using Relational Tables to perform dispatch).

Meta Object Protocol (this is about way more than dispatch, but dispatch is one facet of Common Lisp's MOP).

Equally for reference, I do not program in Smalltalk, C++, Java or any other overtly OO language.

What do you program in? Haskell's Pattern Matching is an example of multiple dispatch; one which is frequently resolvable at Compile Time.


Could this, should this, be turned into a simple, clear, single-purposed Document Mode page explaining the concept?

Wouldn't Dynamic Dispatch be the right place for that? I started trying to rewrite it, but here's the problem: The concept of Dynamic Dispatch is really, really simple, because it's a very vague generalization of a feature that appears in many languages. However, the details and terminology of that feature differ vastly among the different languages, so it's hard to describe Dynamic Dispatch in a concrete way that will immediately click for every programmer. After you've seen the feature in a number of languages, the generalization will seem obvious once pointed out.

A very general description might be something like: "A means of selecting a function to call from among a candidate set of functions, based on the types and/or values of the arguments in the call. The candidate set can often be determined statically, based on the function name, although in some languages it can be based on both the function name, the number of arguments, and the statically-determinable types of the arguments. The types or values used for final selection of a call target must, in the general case, be determined at runtime, hence the selection is dynamic."

You'll note that you can't even characterize dynamic dispatch as being dependent on argument types, because some languages allow a method to be selected entirely or partially based on values, not types.


See original on c2.com