Compile-time Polymorphism
   HOME

TheInfoList



OR:

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, e ...
, 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 in other languages, in conjunction with
function overloading In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
(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, in order to instantiate specific versions of
generic function In computer programming, a generic function is a function defined for polymorphism. In statically typed languages In statically typed languages (such as C++ and Java), the term ''generic functions'' refers to a mechanism for ''compile-time pol ...
s, and select specific function calls based on the supplied definitions. This contrasts with
dynamic dispatch In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented ...
, 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 In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented ...


References

* https://developer.apple.com/swift/blog/?id=27 Polymorphism (computer science) {{prog-lang-stub