Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow equality operator for packed structs #21678

Closed
andrewrk opened this issue Oct 12, 2024 · 2 comments · Fixed by #21679
Closed

allow equality operator for packed structs #21678

andrewrk opened this issue Oct 12, 2024 · 2 comments · Fixed by #21679
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

const std = @import("std");
const expect = std.testing.expect;

test "packed struct equality" {
    const S = packed struct {
        a: u4,
        b: u4,
    };
    const x: S = .{ .a = 1, .b = 2 };
    const y: S = .{ .b = 2, .a = 1 };
    try expect(x == y);
}
test.zig:11:18: error: operator == not allowed for type 'test.test.packed struct equality.S'
    try expect(x == y);
               ~~^~~~
test.zig:5:22: note: struct declared here
    const S = packed struct {
              ~~~~~~~^~~~~~

This test should pass, for the same reason that equality is allowed for enums.

@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Oct 12, 2024
@andrewrk andrewrk added this to the 0.15.0 milestone Oct 12, 2024
@T-727
Copy link

T-727 commented Oct 12, 2024

In my opinion it would be better to implement @backingInt and compare the result of that.
reasoning:
A) clear intent
B) no operator special case for what is a sub-type of structs
C) If both features are accepted we have too many ways to do the same

@rohlem
Copy link
Contributor

rohlem commented Oct 12, 2024

C) If both features are accepted we have too many ways to do the same

@T-727 I disagree here: If Zig allows you to write both @backingInt(a) == @backingInt(b) and a == b, then the latter still is the more obvious way to do things.
Additionally, I assume that the proposed equality is only defined if @TypeOf(a) == @TypeOf(b) (also asked this in #21679 (comment)) -
in that case @backingInt loses that type safety and is clearly inferior if both would be valid.

That in turn negates your point A) - the intent of a == b is clearer,
because the intent behind @backingInt(a) == @backingInt(b) may well be to compare the backing integer / in-memory representation of possibly-distinct types.
That may however still be the intent for certain use cases, making the ability to express it still valuable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants