Hi! I guess packed structs of bools are the way to do bit flags in Zig.
For some things this is fine and readable, but some things look a bit ugly compared to how I’d write it in C. For example:
pub const Result = packed struct(u8) {
active: bool,
submit: bool,
change: bool,
_: u5 = 0,
pub const none: Result = @bitCast(@as(u8, 0));
};
pub fn functionReturningResult() Result {
// .. longer function impl here
var res = Result.none;
res.active = true;
return res;
}
In C, the last 3 lines of the functionReturningResult() would be something like return RES_ACTIVE;.
Alternatively, I could default the fields to false and then do something like return .{ .active = true }; but that’s a little busy too.
Any alternatives or nicer ways of doing the above? FWIW, I’m trying to avoid comptime tricks with this one, as don’t want to throw off ZLS.