In Zig, vtables (virtual tables) are a manual pattern used to implement dynamic dispatch, allowing different types to be used through a single, shared interface. [^1][^2][^3][^4][^5] Unlike languages like C++ or Java, Zig does not have "classes" or "interfaces" as built-in keywords. Instead, you construct vtables yourself by creating a struct of function pointers. [^6][^7][^8][^9][^10]
A typical Zig interface is a "fat pointer" consisting of two main parts: [^3][^11]
- A Context Pointer (ptr): A pointer to the specific implementation's data, usually typed as
*anyopaque. - A VTable Pointer (vtable): A pointer to a constant struct containing function pointers that define the interface's behavior. [^3][^12][^13]
The most prominent use of this pattern in the Zig Standard Library is the Allocator. It allows functions to accept any allocator (like ArenaAllocator or GeneralPurposeAllocator) without knowing exactly which one is being used at co