I am trying to create enums that have an additional "negative" value for each value in the original enum.
I can generate said enums by creating additional fields with suffixed names.
I would also like to have a "negation" operation that switches between suffixed and non-suffixed members.
Not that important, but I would prefer if there was no way to create negative fields without calling "negate" on their counterparts.
const fields = @typeInfo(T).Enum.fields;
const multiplier = 2;
var enumFields: [fields.len * multiplier]std.builtin.Type.EnumField = undefined;
var decls = [_]std.builtin.Type.Declaration{};
inline for (fields, 0..) |field, i| {
const base = i * multiplier;
enumFields[base + 0] = .{
.name = field.name,
.value = base + 0,
};
enumFields[base + 1] = .{
.name = field.name ++ "_",
.value = base + 1,
};
}
return @Type(.{
.Enum = .{
.tag_type = std.math.IntFittingRange(0, fields.len * multiplier - 1),
.fields = &enumFields,
.decls = &decls,
.is_exhaustive = true,
},
});
}
Problem is, I don't know how I would declare a functions for generated enums.
The code above only works if I leave decls empty.
Otherwise, it is failing with error message "reified enums must have no decls."
This example is more how I would like it to be:
pub const baz = WithNegation(foo);
pub const biz = enum {
bar,
bar_,
pub fn negate(self: biz) biz {
switch (self) {
.bar => return .bar_,
.bar_ => return .bar,
}
}
};```
Is there a way to do what I want?