Std.mem.eql seems to not work

I was working on a personal project called freightdb. I took a two month break from coding, but when I came back I stated to work from where I left of, a function which return a slice of Nodes(It is in the repo),

This is the un-commited code (This is actually the second version of this code, the first on ewas too buggy and I removed it of the repo):

        pub fn filterNodesByField(self: *Self, active_tag: []const u8, field_name: []const u8, value: anytype) ![]const Node {
            const nodes = try self.fetchNodes(active_tag);

            var list = std.ArrayList(Node).init(self.allocator);
            defer list.deinit();

            for (nodes) |node| {
                switch (node) {
                    inline else => |n| {
                        if (!checkField(@TypeOf(n), field_name))
                            return error.InvalidField;

                        inline for (std.meta.fields(@TypeOf(n))) |field| if (std.mem.eql(u8, field.name, field_name)) {
                            if (@TypeOf(@field(n, field.name) != @TypeOf(value)))
                                return error.InvalidArgument;

                            if (@field(n, field.name) == value)
                                try list.append(n);
                        };
                    },
                }
            }

            return list.items;
        }

I wrote a test for it in the repo, and it is still in there. Most of the errors and bugs of the first version is gone, but one remains:

.codeberg/freightdb/src/graph.zig:195:63: error: incompatible types: 'tag.Tag' and 'type'
                            if (@TypeOf(@field(n, field.name) != @TypeOf(value)))
                                        ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
.codeberg/freightdb/src/graph.zig:195:41: note: type 'tag.Tag' here
                            if (@TypeOf(@field(n, field.name) != @TypeOf(value)))
                                        ^~~~~~~~~~~~~~~~~~~~~
.codeberg/freightdb/src/graph.zig:195:66: note: type 'type' here
                            if (@TypeOf(@field(n, field.name) != @TypeOf(value)))
                                                                 ^~~~~~~~~~~~~~
referenced by:
    test.Graph: filterNodesByField: .codeberg/freightdb/src/graph.zig:330:47
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

The compiler is saying that the type of the field tag is not compatable with the type of value I gave it. But from this line : if (std.mem.eql(u8, field.name, field_name)) I expected it to only check the types if the name matches, but it is not the case, I don’tunderstand why,

Additional info

The funtion is inside of the graph type

Looks like you just have a parenthesis typo?

if (@TypeOf(@field(n, field.name) != @TypeOf(value))) {

should be

if (@TypeOf(@field(n, field.name)) != @TypeOf(value)) {
2 Likes