Hey all,
I am currently on a quest to learn idiomatic* Zig and I am doing so by building a tiny compiler.
When using tagged unions, I find myself using std.meta.activeTag a lot.
For example,
const std = @import("std");
pub const Type = enum {
number,
boolean,
};
pub const Value = union(Type) {
number: f32,
boolean: bool,
pub fn equals(self: Value, other: Value) bool {
if (std.meta.activeTag(self) != std.meta.activeTag(other)) return false;
return switch (self) {
.number => |n| n == other.number,
.boolean => |b| b == other.boolean,
};
}
};
Is this expected? Are there patterns that would allow me not to?
Reaching for meta that often makes me feel like I am doing something wrong, but maybe it's entirely expected.
*: I have been playing with the language for a while, I am really trying to understand how Zig "ought to be written", more than "what's possible", if that makes any sense.