In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, ...
, static dispatch is a form of
polymorphism
Polymorphism, polymorphic, polymorph, polymorphous, or polymorphy may refer to:
Computing
* Polymorphism (computer science), the ability in programming to present the same programming interface for differing underlying forms
* Ad hoc polymorphis ...
fully resolved during
compile time
In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled.
The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
. It is a form of ''method dispatch,'' which describes how a language or environment will select which implementation of a method or function to use.
Examples are
templates in C++, and
generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
in other languages, in conjunction with
function overloading (including
operator overloading). Code is said to be
monomorphised, with specific
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
s deduced and traced through the
call graph
A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge ''(f, g)'' indicates that procedure ''f'' call ...
, in order to instantiate specific versions of
generic functions, and select specific function calls based on the supplied definitions.
This contrasts with
dynamic dispatch, which is based on runtime information (such as
vtable pointers and other forms of run time type information).
Static dispatch is possible because there is a guarantee of there only ever being a single implementation of the method in question. Static dispatch is typically faster than dynamic dispatch which by nature has higher overhead.
Example in Rust
In
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH), ...
.
trait Speak
struct Cat;
impl Speak for Cat
fn talk(pet: T)
fn main()
Rust will monomorphize this when compiled into:
fn talk_cat(pet: Cat)
See also
*
Dynamic dispatch
References
* https://developer.apple.com/swift/blog/?id=27
Polymorphism (computer science)
{{prog-lang-stub