Swift Dispatch methods

Asked on 08/12/2024

1 search

In Swift, dispatch methods refer to how function calls are resolved at runtime. There are two main types of dispatch in Swift: static dispatch and dynamic dispatch.

  1. Static Dispatch: This occurs when the compiler knows at compile time exactly which function is being called. Static dispatch is more efficient because it allows for optimizations like inlining and generic specialization. In Swift, calls to functions declared in protocol extensions use static dispatch.

  2. Dynamic Dispatch: This is used when the exact function to be called is determined at runtime. Dynamic dispatch is necessary for polymorphism and is used for calls to protocol requirements. In Swift, if a method is declared in the main body of a protocol, the call to it uses dynamic dispatch.

For more details, you can refer to the session Explore Swift performance (05:54) where these concepts are discussed.