Middle — Middle+
In what situations is a Witness Table used in programming?
sobes.tech AI
Answer from AI
Witness Table is used in programming languages that support protocols or interfaces, such as Swift. It serves to implement dynamic binding of protocol methods to specific types that implement the protocol.
When a type conforms to a protocol, the witness table contains pointers to the implementations of that protocol's methods for the given type. This allows calling the correct methods at runtime, even if the type is only known through the protocol.
Examples of usage:
- In Swift when using protocols with dynamic dispatch.
- When implementing polymorphism and generics, where the specific type is only known at runtime.