Error Union

Noun · Development

Definitions

  1. A Zig type that holds either a normal value or an error, written ErrorSet!T, or !T when the compiler should infer the error set. Errors in Zig are plain values drawn from named error sets, not exceptions, so there is no hidden control flow and no allocation when one occurs. Callers must deal with an error union before they can use the value: try unwraps it or returns the error to the caller, catch supplies a fallback or handler, and if with a capture branches on success or failure. errdefer schedules cleanup that runs only when a function exits with an error, which makes partial-initialisation cleanup straightforward. Error sets can be merged with the || operator, and in debug builds Zig records error return traces showing the path an error travelled. The design is similar in spirit to Rust's Result type but lighter, since error values are small integers the compiler assigns.

    In plain English: A return type in Zig that says 'this gives you either the result you want OR an error' — and the compiler won't let you ignore the error.

    Example: "The function returns FileOpenError!File — you must handle the error before you can use the file handle."

Related Terms