Static dispatch

In computing, static dispatch is a form of polymorphism fully resolved during 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.[1]

Examples are templates in C++, and generic programming in Fortran and other languages, in conjunction with function overloading (including operator overloading). Code is said to be monomorphised, with specific data types deduced and traced through the call graph, 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.

Examples

Consider the following Rust program:[2]

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);
}

the Rust Compiler will monomorphize the program's code at compile time into:

// [...]
// 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
}

[Note 1]

See also

Notes

  1. ^ Keep in mind that further optimizations might remove "talk_cat", opting to inline it into a direct call to the struct's impl like so:
    fn main() {
        let pet = Cat::new(String::from("Simba"));
        Cat::speak(&pet);
    }
    

    In which case "talk_cat" and "talk" will be omitted from the final optimized output due to dead code elimination.

References

  1. ^ Elements of Clojure. Lulu.com. 2019. p. 68. ISBN 9780359360581. Retrieved 17 July 2022.
  2. ^ "Generic Data Types - The Rust Programming Language". doc.rust-lang.org.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.