0Pricing
Zig Academy · Lesson

Inferred Error Sets

Let Zig collect your errors for you.

Inferred Error Sets is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Listing Errors Is Tedious

Naming every error a function might return can be a chore, especially when it calls other fallible functions. Zig can infer the set.

Leave the Set Off

Write just !T with nothing before the bang. Zig studies the function body and figures out every error it can produce.

fn parse(s: []const u8) !u32 {
    // ...
}

The Compiler Collects Them

Each try and each returned error inside the body is gathered automatically into one inferred error set for that function.

if (s.len == 0) return error.Empty;

It Stays Accurate

Add a new failing call later and the inferred set grows to include it. You never forget to update a hand-written list.

Inference Across Calls

If your function uses try on another fallible call, that callee's possible errors are folded into your inferred set as well.

const f = try open(path);

Explicit Sets Still Win

Sometimes you want a fixed public contract. Naming the set before ! forbids any error outside it, which inference will not do.

fn load() FileError!Data {}

Trade-Off in One Line

Inferred sets are convenient and self-updating; explicit sets are precise and stable. Pick by whether the contract must stay frozen.

Inspect with @errorReturnTrace

You can still reason about errors at runtime. In debug builds Zig keeps an error return trace showing where a failure began.

Great for Internal Helpers

Inference shines on private helper functions where a strict public API is not needed. Let Zig handle the bookkeeping there.

Still Fully Checked

Inferred or explicit, every error must still be handled by callers. Inference saves typing, not safety; the compiler stays strict.

A Pragmatic Default

Many Zig functions simply use !T and let inference work. Reserve named sets for the boundaries you want to lock down.

Quick Check

A function is declared with the return type !u32 and no error set before the bang. What does Zig do?

Recap

You saw inferred error sets via bare !T, how they grow with the body, and when an explicit set is the better choice. ✅

Frequently asked questions

Is the “Inferred Error Sets” lesson free?

Yes — the full text of “Inferred Error Sets” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Inferred Error Sets”?

Let Zig collect your errors for you. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inferred Error Sets” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Error Sets and the !T Union
  2. Returning Errors from Functions
  3. Propagate Failures with try
  4. Inferred Error Sets
← Back to Zig Academy