The Perennial Enum Handling Problem

When working with algebraic data types (enums, tagged unions, or variants), developers often need to execute shared logic for multiple variants before handling case-specific behavior. Traditional approaches force compromises:

// Rust example: Duplicated logic
match u {
    U::A(_) => {
        handle_ab(); // Repeated
        handle_a();
    }
    U::B(_) => {
        handle_ab(); // Repeated
        handle_b();
    }
    U::C => handle_c(),
}

Refactoring the enum hierarchy (U → AB + C) often proves impractical, especially in large codebases. The fallback—runtime panics via unreachable!()—works but introduces theoretical brittleness:

match u {
    U::A(_) | U::B(_) => {
        handle_ab();
        match u { // Nested match
            U::A(_) => handle_a(),
            U::B(_) => handle_b(),
            _ => unreachable!(), // ❌ Runtime risk
        }
    }
    U::C => handle_c(),
}

Zig's Compile-Time Solution

Zig leverages its comptime and inline capabilities to eliminate this hazard. By resolving variant handling at compile time, it maintains safety without restructuring enums:

const U = union(enum) {
    a: i32,
    b: i32,
    c,
};

fn handle(u: U) void {
    switch (u) {
        inline .a, .b => |_, tag| { // Unrolls at compile time
            handle_ab();
            switch (tag) {
                .a => handle_a(),
                .b => handle_b(),
                else => comptime unreachable, // ✅ Compile-time proof
            }
        },
        .c => handle_c(),
    }
}

Why This Matters

  • Zero Runtime Overhead: inline generates specialized code for each variant during compilation.
  • Compiler-Enforced Safety: comptime unreachable fails compilation if the else branch could ever execute, catching refactoring errors early.
  • Preserved Enum Structure: Avoids invasive type hierarchy changes that complicate codebases.

The Power of Zig's Meta-Programming

This pattern exemplifies Zig's philosophy: providing low-level control with high-level ergonomics through compile-time execution. As matklad's analysis shows, attempting to add an unsupported variant (like .c) to the inline block immediately fails compilation—proving the impossibility of runtime errors. For developers wrestling with enum ergonomics in systems programming, Zig offers not just solutions but verifiable guarantees.

Source: matklad.github.io