Why is enum equality check comptime?

Hi, I’m a relative zig newbie scratching my head at a compilation error. Cribbing heavily from the zig guide, I have crafted the following enum:

const DBPageType = enum(u8) {
    index_int = 0x02,
    table_int = 0x05,
    index_leaf = 0x0a,
    table_leaf = 0x0d,

    pub fn isInterior(self: DBPageType) bool {
        return (self == DBPageType.index_int) || (self == DBPageType.table_int);
    }
};

However when I compile, I am confronted with the following error:

src/main.zig:119:22: error: unable to evaluate comptime expression
        return (self == DBPageType.index_int) || (self == DBPageType.table_int);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
src/main.zig:119:17: note: operation is runtime due to this operand
        return (self == DBPageType.index_int) || (self == DBPageType.table_int);
                ^~~~
src/main.zig:119:16: note: types must be comptime-known
        return (self == DBPageType.index_int) || (self == DBPageType.table_int);

I was able to work around it by unrolling the expression into chained if statements, but it’s still unclear to me why this error occurs. Particularly, why does enum equality in this instance need to be comptime-known, but this restriction does not apply if I compare it one at a time in an if statement?

1 Like

It’s quite simply because you used || (the error set merge operator) instead of or. And the compiler error is really misleading, here is the corresponding issue on github: Misleading error when using `||` instead of `or` with comparisons · Issue #15526 · ziglang/zig · GitHub

8 Likes