Union Equality

I think this is kind of neat:

    pub fn equals(self: Value, other: Value) bool {
        return switch (self) {
            inline else => |value, tag| tag == std.meta.activeTag(other) and value == @field(other, @tagName(tag)),
        };
    }

This works because for nil it just compares that {} is equal to {} which is true.


If any case should compare values differently like for example float, you can add it as an explicit case like this:

    pub fn equals(self: Value, other: Value) bool {
        return switch (self) {
            .float => |f| .float == std.meta.activeTag(other) and floatEqualImpl(f, other.float),
            inline else => |value, tag| tag == std.meta.activeTag(other) and value == @field(other, @tagName(tag)),
        };
    }
8 Likes