What is useful about an empty enum value?

I am wondering when an empty enum is useful, it seems like it is a strange value for an enum, trying to print it gives me ‘cannot get @tagName of empty enum’, but @comptimeLog prints ‘(empty enum value)’.

Is empty enum allowed so that there is some kind of zero element, to simplify something about implementation, not having to special case?
Does it have similarity to u0?

Are there other interesting properties about this value?

const std = @import("std");

pub fn main() !void {
    const val: enum {} = undefined;
    @compileLog(val);
    std.debug.print("val: {}\n", .{val});
}
3 Likes

I’m not sure, but I did notice that there was a relevant proposal that was recently accepted:

4 Likes

I was looking for a return type that can be used to indicate that the caller doesn’t need to wait for the execution of a function to finish. If enum{} is going to get changed into noreturn, then I guess have to use enum{no_wait} instead.

That’s a better choice anyway. An enum of no types might work in a structurally-typed language, but in Zig one enum {} is different from the next enum {}, so it’s an awkward construct. Doing anything but comptime reflection with it is a compile error. Even if it was a singleton, there’s no justification for assigning the null enum to a particular role.

An enum inhabited by one tag, like const NoWait = enum{no_wait};, is backed by a u0, so it’s “free” in that sense, for whatever sort of metaprogramming you’re doing. The return instructions for the function should be the same as using void as a return value.

4 Likes

Yeah, it makes more sense semantically. More obvious what the declaration means than enum{}.

I’m working on adding support for function pointers to my project. The function being called would actually be a JavaScript function, which has to run in the main thread. When function is called from a different thread, I need to pause that thread and await the processing of the call. When the function isn’t actually returning a useful value, then that’s not really necessary. If the function is being called for logging purpose, for example.

1 Like