All Topics  
Dynamic dispatch

 

   Email Print
   Bookmark   Link






 

Dynamic dispatch



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, dynamic dispatch is the process of mapping a message
Message passing

Message passing in computer science, is a form of communication used in parallel computing, object-oriented programming, and interprocess communication....
 to a specific sequence of code (method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
) at runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
. This is done to support the cases where the appropriate method cannot be determined at compile-time (i.e. statically). Dynamic dispatch is only used for code invocation and not for other binding processes (such as for global variables) and the name is normally only used to describe a language feature where a runtime decision is required to determine which code to invoke.



Dynamic dispatch is needed when multiple classes
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 contain different implementations of the same method foo.






Discussion
Ask a question about 'Dynamic dispatch'
Start a new discussion about 'Dynamic dispatch'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, dynamic dispatch is the process of mapping a message
Message passing

Message passing in computer science, is a form of communication used in parallel computing, object-oriented programming, and interprocess communication....
 to a specific sequence of code (method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
) at runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
. This is done to support the cases where the appropriate method cannot be determined at compile-time (i.e. statically). Dynamic dispatch is only used for code invocation and not for other binding processes (such as for global variables) and the name is normally only used to describe a language feature where a runtime decision is required to determine which code to invoke.

Single and multiple dispatch



Dynamic dispatch is needed when multiple classes
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 contain different implementations of the same method foo. If the class of an object x is not known at compile-time, then when x.foo is called, the program must decide at runtime which implementation of foo to invoke, based on the runtime type of object x. This case is known as single dispatch because an implementation is chosen based on a single type—that of the type of the instance
This (computer science)

In many object-oriented programming programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working....
. Single dispatch is supported by many object-oriented languages, including statically typed languages such as C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 and Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, and dynamically typed languages such as Smalltalk
Smalltalk

Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
 and Objective-C
Objective-C

Objective-C is a Reflection , Object-oriented programming programming language which adds Smalltalk-style message passing to C .Today it is used primarily on Mac OS X, iPhone OS, and GNUstep, three environments based on the OpenStep standard, and is the primary language used for the NEXTSTEP, OpenStep#OPENSTEP, and Cocoa application framew...
.

In a small number of languages such as Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
, methods or functions can also be dynamically dispatched based on the type of arguments. Expressed in pseudocode, the code manager.handle(y) could call different implementations depending on the type of object y. This is known as multiple dispatch.

Dynamic dispatch mechanisms

A language may be implemented with different dynamic dispatch mechanisms. The choices of the dynamic dispatch mechanism offered by a language to a large extent alter the programming paradigms that are available or are most natural to use within a given language.

Normally in a typed language the dispatch mechanism will be performed based on the type of the arguments (most commonly based on the type of the receiver of a message). This might be dubbed 'per type dynamic dispatch'. Languages with weak or no typing systems often carry a dispatch table as part of the object data for each object. This allows instance behaviour as each instance may map a given message to a separate method.

Some languages offer a hybrid approach.

Dynamic dispatch will always incur an overhead so some languages offer the option to turn dynamic dispatch off for particular methods.

C++ Implementation

C++ uses a virtual table which defines the message to method mapping for a given class. Instances of that type will then store a pointer to this table as part of their instance data. This is complicated when multiple inheritance is used. The virtual table in a C++ object cannot be modified at run-time which limits the potential set of dispatch targets to a finite set chosen at compile-time.

Although the overhead involved in this dispatch mechanism is low, it may still be significant for some application areas that the language was designed to target. For this reason, Bjarne Stroustrup
Bjarne Stroustrup

Bjarne Stroustrup is a computer scientist at the College of Engineering Chair Professor of Computer Science at Texas A&M University. He is most notable for developing the C++ programming language....
, the designer of C++, elected to make dynamic dispatch optional and non-default. Only functions declared with the virtual keyword
Keyword (computer programming)

In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords ? and, indeed, the meaning of the notion of keyword ? differs widely from language to language....
 will be dispatched based on the runtime type of the object; other functions will be dispatched based on the object's static type.

As it is possible to store function pointers as part of an object's data, instance behaviour can be used within a C++ program.

Type overloading does not produce dynamic dispatch in C++ as the language considers the types of the message parameters part of the formal message name. This means that the message name the programmer sees is not the formal name used for binding.

Smalltalk Implementation

Smalltalk uses a type based message dispatcher. Each instance has a single type whose definition contains the methods. When an instance receives a message, the dispatcher looks up the corresponding method in the message-to-method map for the type and then invokes the method.

Smalltalk's mechanism has a significantly higher overhead than that of C++ or Java and this overhead is incurred for each and every message that an object receives. It is possible to alter the Smalltalk message dispatcher to allow instance behaviour, but this is not normally done as it would make the message dispatch even less efficient.

Many other dynamically typed languages, including Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, Ruby, Objective-C
Objective-C

Objective-C is a Reflection , Object-oriented programming programming language which adds Smalltalk-style message passing to C .Today it is used primarily on Mac OS X, iPhone OS, and GNUstep, three environments based on the OpenStep standard, and is the primary language used for the NEXTSTEP, OpenStep#OPENSTEP, and Cocoa application framew...
 and Groovy use similar approaches.

JavaScript Implementation

JavaScript
JavaScript

JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard....
 stores the methods as part of the normal instance data. When an object is defined, methods are loaded into it at any point and may be changed at any time. This allows for great flexibility in the object models used to implement systems with the object definitions being determined at runtime.

There is a separate mechanism that allows a class template to be created with a single constructor and to have default methods attached to it. These default methods will then be available to every instance of that type.

See also


  • Message passing
    Message passing

    Message passing in computer science, is a form of communication used in parallel computing, object-oriented programming, and interprocess communication....
  • Dynamic binding
    Dynamic binding

    In object oriented programming, dynamic binding refers to determining the exact implementation of a request based on both the request name and the receiving object at the run-time....