# Static dispatch

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Static_dispatch
> Markdown URL: https://mediated.wiki/source/Static_dispatch.md
> Source: https://en.wikipedia.org/wiki/Static_dispatch
> Source revision: 1355364761
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|During compilation, selecting which implementation of a method or function to use}}
{{more citations needed|date=January 2021}}
{{Polymorphism}}
<!--- Perhaps a redirect to [Inheritance_(object-oriented_programming)#Uninheritable_classes](/source/Inheritance_(object-oriented_programming))
this article is a bit messy
--->

In [computing](/source/computing), '''static dispatch''' is a form of [polymorphism](/source/polymorphism_(computer_science)) fully resolved during [compile time](/source/compile_time). 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.<ref>{{cite book |year=2019 |title=Elements of Clojure|publisher=Lulu.com|publication-date=2019 |url=https://books.google.com/books?id=JSiLDwAAQBAJ&dq=%22Static+dispatch%22+-wikipedia&pg=PA68|access-date=17 July 2022 | page=68|isbn=9780359360581 }}</ref>

Examples are [templates in C++](/source/Template_(C%2B%2B)), and [generic programming](/source/generic_programming) in [Fortran](/source/Fortran) and other languages, in conjunction with [function overloading](/source/function_overloading) (including [operator overloading](/source/operator_overloading)). Code is said to be [monomorphised](/source/Monomorphism_(computer_science)), with specific [data type](/source/data_type)s deduced and traced through the [call graph](/source/call_graph), in order to instantiate specific versions of [generic function](/source/generic_function)s, and select specific function calls based on the supplied definitions.

This contrasts with [dynamic dispatch](/source/dynamic_dispatch), which is based on runtime information (such as [vtable](/source/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.

==Examples==
Consider the following [Rust](/source/Rust_(programming_language)) program:<ref>{{cite web |title=Generic Data Types - The Rust Programming Language |url=https://doc.rust-lang.org/book/ch10-01-syntax.html#performance-of-code-using-generics |website=doc.rust-lang.org}}</ref>

<syntaxhighlight lang="rust" line="1">trait Pet {
    fn speak(&self);
}

struct Cat {
    name: String
}

impl Cat {
    fn new(name: String) -> Self {
        Cat { name }
    }
}

impl Pet for Cat {
    fn speak(&self) {
        println!("{} says Meow!", self.name);
    }
}

fn talk<T: Pet>(pet: T) {
    pet.speak();
}

fn main() {
    let pet = Cat::new(String::from("Simba"));
    talk(pet);
}</syntaxhighlight>

the Rust Compiler will [monomorphize](/source/Monomorphization) the program's code at compile time into:

<syntaxhighlight lang="rust" line="1" start="18">// [...]
// struct Cat, Cat's impl, the Pet impl for struct Cat, and the Pet trait remain the same.
fn talk_cat(pet: Cat) {
    Cat::speak(&pet)
}

fn main() {
    let pet = Cat::new(String::from("Simba"));
    talk_cat(pet); // talk(pet) gets replaced with the more specialized talk_cat
}</syntaxhighlight><ref name=InliningExp group=Note/>
== See also ==
{{portal|Computer programming}}
* [Dynamic dispatch](/source/Dynamic_dispatch)
== Notes ==
{{reflist|group=Note|refs=
<ref name=InliningExp group=Note>
Keep in mind that further optimizations might remove "talk_cat", opting to [inline](/source/Inline_expansion) it into a direct call to the struct's impl like so:
<syntaxhighlight lang="rust" line="1" start="25">
fn main() {
    let pet = Cat::new(String::from("Simba"));
    Cat::speak(&pet);
}</syntaxhighlight>
In which case "talk_cat" and "talk" will be omitted from the final optimized output due to [dead code elimination](/source/Dead-code_elimination).
</ref>
}}
==References==
{{Reflist}}
* https://developer.apple.com/swift/blog/?id=27

Category:Polymorphism (computer science)
{{prog-lang-stub}}

---
Adapted from the Wikipedia article [Static dispatch](https://en.wikipedia.org/wiki/Static_dispatch) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Static_dispatch?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
