fbpx
Wikipedia

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.

Example in Rust edit

In Rust.[2]

trait Speak {  fn speak(&self); } struct Cat; impl Speak for Cat {  fn speak(&self) {  println!("Meow!");  } } fn talk<T: Speak>(pet: T) {  pet.speak(); } fn main() {  let pet = Cat;  talk(pet); } 

Rust will monomorphize this when compiled into:

fn talk_cat(pet: Cat) {  pet.speak(); } 

See also edit

References edit

  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.


static, dispatch, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, citations, reliable, sources, unsourced, material, challenged, removed, find, sources, news, newspapers, books, scholar, jstor, january, . This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Static dispatch news newspapers books scholar JSTOR January 2021 Learn how and when to remove this template message 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 Example in Rust editIn Rust 2 trait Speak fn speak amp self struct Cat impl Speak for Cat fn speak amp self println Meow fn talk lt T Speak gt pet T pet speak fn main let pet Cat talk pet Rust will monomorphize this when compiled into fn talk cat pet Cat pet speak See also editDynamic dispatchReferences edit Elements of Clojure Lulu com 2019 p 68 ISBN 9780359360581 Retrieved 17 July 2022 Generic Data Types The Rust Programming Language doc rust lang org https developer apple com swift blog id 27 nbsp This programming language related article is a stub You can help Wikipedia by expanding it vte Retrieved from https en wikipedia org w index php title Static dispatch amp oldid 1189667290, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.