A pattern for error reporting in Zig that uses factory functions instead of error enums. Rather than defining error types as sum types, create constructor functions that handle error creation and formatting. This decouples error emission from error reporting, enables polymorphism (errors can be printed to stderr or collected in memory), and provides flexibility to change implementation without affecting call sites. The approach leverages the duality between sum types and function products (visitor pattern) while optimizing interfaces for both producers and consumers of errors.

3m read timeFrom matklad.github.io
Post cover image

Sort: