# Union Equality

**URL:** <https://ziggit.dev/t/union-equality/4060>\
**Category:** Help\
**Tags:** language\
**Created:** [April 23, 2024, 4:03pm UTC](https://ziggit.dev/t/union-equality/4060 "2024-04-23T16:03:59Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Sze](https://ziggit.dev/user_avatar/ziggit.dev/sze/32/496_2.png) [@Sze](https://ziggit.dev/u/Sze)\
**Post date:** [April 23, 2024, 9:43pm UTC](https://ziggit.dev/t/union-equality/4060/7 "2024-04-23T21:43:18Z")

</div>

I think this is kind of neat:

```zig
    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:

```zig
    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)),
        };
    }

```

---

_[View the full topic](https://ziggit.dev/t/union-equality/4060)._
