Basically, I had a struct with only one field and decided to turn it into an enum instead, because I had quite a few constants of type @This() which I thought could be more clear and terse as enum fields, but I ran into a problem turning the constants into fields. Below is a minimal reproduction of the problem I ran into. Why do these fields create circular dependency errors when the constant declarations don’t? Because this is an enum, aren’t they practically the same? Is it better to use a struct with a single field instead of a non-exhaustive enum?
test.zig:
pub const E = enum(u32) {
/// What this function does doesn't matter
pub fn foo(x: E) u32 {
return @intFromEnum(x) + 100;
}
pub const this_works: E = @enumFromInt(foo(.zero));
pub const this_works_too: E = @enumFromInt(foo(.one));
pub const zero: E = @enumFromInt(0);
one = 1,
this_doesnt_work = foo(.zero),
this_doesnt_work_either = foo(.one),
_,
};
test {
@import("std").testing.refAllDecls(E);
}
console output:
$ zig test test.zig
error: dependency loop with length 2
test.zig:13:24: note: type 'test.E' uses value of declaration 'test.E.foo' here
this_doesnt_work = foo(.zero),
^~~
test.zig:3:16: note: value of declaration 'test.E.foo' depends on type 'test.E' for function parameter declared here
pub fn foo(x: E) u32 {
^~~~
note: eliminate any one of these dependencies to break the loop