Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Method Dispatch?

What is Method Dispatch? I can find several concrete examples, but an abstract definition of method dispatch eludes me. Anyone care to venture theirs?

like image 764
ProfK Avatar asked Nov 26 '09 20:11

ProfK


People also ask

What is method dispatch in Java?

Dynamic method dispatch is the mechanism in which a call to an overridden method is resolved at run time instead of compile time. This is an important concept because of how Java implements run-time polymorphism.

What is method dispatch in Swift?

Method dispatch refers to the operation a program uses to determine which method implementation should be executed when a method or function is called. The two most well-known dispatch types we have in Swift are the Static and the Dynamic one.

What is a dispatch in programming?

The dispatcher is the module that gives a process control over the CPU after it has been selected by the short-term scheduler. This function involves the following: Switching context. Switching to user mode. Jumping to the proper location in the user program to restart that program.


2 Answers

First let's say what a message and a method are:

  • A message is a name that can be sent from one object to another, possibly with additional objects as arguments. For example in

    account withdraw: 100 

    The message is withdraw: (Smalltalk syntax.) (Other languages might write account.withdraw(100).) The object receiving the message, in this example account, is called the receiver.

  • A method is an implementation that can be invoked in response to a message.

These ideas are shared among a wide variety of object-oriented languages, sometimes under different names. For example, C++ calls a message a 'virtual member function'.

Now:

  • Method dispatch is the algorithm used to decide which method should be invoked in response to a message. Algorithms vary dramatically across languages:

    • Languages like Smalltalk, which have classes and single inheritance, consult the class of the receiver. If the method is defined on that class, that method is invoked. Otherwise the algorithm checks the unique superclass, and so on.

    • In C++, the method is still determined by the class of the receiver, but because a class can have multiple superclasses, the problem of deciding which method to invoke is more complicated.

    • In languages like Self, which have methods but no classes, the method is either found in a named slot on the receiver itself, or possibly is found in the prototype from which the object was cloned.

    • In more advanced object-oriented languages, the method-dispatch algorithm examines not only the receiver but the arguments that are passed along with the message. This idea is sometimes referred to as 'multimethods'. (To a degree, this technique can be simulated using what Smalltalk calls double dispatch, but there's a programming cost and a performance cost.) I believe the languages Cecil, Diesel, and Dylan all use some form of multimethod dispatch, but I'm teetering on the edge of my expertise.

like image 127
Norman Ramsey Avatar answered Nov 10 '22 08:11

Norman Ramsey


It's hard to say without a context, but I'd describe it as the process which takes a method invocation in source code, decides which method requires executing, and executes it, performing any argument conversions, defaulting etc as required by the language.

The decision part of method dispatch may be purely at execution time (e.g. in a dynamic language), purely at compile time (e.g. calling a static method in C#/Java), or both (calling a virtual method in C#/Java).

Different languages can have significantly different approaches to method dispatch.

like image 32
Jon Skeet Avatar answered Nov 10 '22 09:11

Jon Skeet