FACTOID # 159: Taiwan and Luxembourg are the only countries in the world where the mobile phones outnumber the people!
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Dynamic dispatch

In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. 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. Computer science is the study of information and computation. ... Some programming languages based on the Actor model or other distributed programming models define messaging as the (usually asynchronous) sending (usually by copy) of a data item to a communication endpoint (Actor, process, thread, socket, ). Such messaging is used to implement various RPC services including SOAP which is used for... Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods or static methods) or with an object (called instance methods). ... In computer science, run time (with a space, though often its spelled without one) describes the operation of a computer program, the duration of its execution, from beginning to termination (compare compile time). ... In computer science, compile time, as opposed to runtime, is the time when a compiler compiles code written in a programming language into an executable form. ...

Contents


Single and multiple dispatch

Main article: multiple dispatch

Dynamic dispatch is needed when multiple classes contain different implementations of the same method foo(). If the type 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 this or self object. Single dispatch is supported by many object-oriented languages, including static typed languages such as C++ and Java, and dynamically typed languages such as Smalltalk and Objective-C. Multiple dispatch or multimethods is the feature of some object-oriented programming languages in which a function or method can be specialized on the type of more than one of its arguments. ... In object-oriented programming, classes are used to group related variables and functions. ... On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose computer programming language. ... Java is an object-oriented programming language developed by James Gosling and colleagues at Sun Microsystems in the early 1990s. ... On computer science, a datatype (often simply type) is a name or label for a set of values and some operations which can be performed on that set of values. ... Smalltalk is an object-oriented, dynamically typed, reflective, programming language designed at Xerox PARC by Alan Kay, Dan Ingalls, Ted Kaehler, Adele Goldberg, and others during the 1970s, influenced by Sketchpad and Simula. ... Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language...


In a small number of languages such as Common Lisp, 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. Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, standardised by ANSI X3. ...


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.


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. A virtual table, or vtable, is a mechanism used in Programming languages to support dynamic polymorphism, i. ...


Although the overhead involved in this dispatch mechanism is low it may still be significant for some application areas that the languages was designed to target. For this reason, Bjarne Stroustrup, the designer of C++, elected to make dynamic dispatch optional and non-default. Only functions declared with the virtual keyword will be dispatched based on the runtime type of the object; other functions will be dispatched based on the object's static type. Bjarne Stroustrup Bjarne Stroustrup (born December 30, 1950 in Aarhus, Denmark) is a computer scientist and the College of Engineering Chair Professor of Computer Science at Texas A&M University. ...


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, Ruby and Objective-C use similar approaches. Python is an interpreted programming language created by Guido van Rossum in 1990. ... Ruby is a reflective, object-oriented programming language. ... Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language...


JavaScript Implementation

JavaScript 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. JavaScript is an object-based scripting programming language based on the concept of prototypes. ...


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.


In this context, the Python programming language is fairly unique in that it also allows object-level binding of methods, next to class-level method lookup. This is a feature not found in Smalltalk, unless reflection techniques are used. Python is an interpreted programming language created by Guido van Rossum in 1990. ...


References

  • Lippman, Stanley B. (1996). Inside the C++ Object Model, Addison-Wesley. ISBN 0-201-83454-5.


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m