I was working on my personal project freightdb, specifically on graph data-structure on a function to update a node, when I run into a error:
src/graph.zig:123:76: error: unable to resolve comptime value
var updated_node: std.meta.TagPayload(Node, std.meta.activeTag(old_node)) = undefined;
^~~~~~~~
src/graph.zig:123:76: note: argument to function being called at comptime must be comptime-known
This is the function:
pub fn updateNode(self: *Self, tag_str: []const u8, comptime field_name: []const u8, value: anytype) !void {
const old_node: Node = try self.deleteNode(tag_str);
var updated_node: std.meta.TagPayload(Node, std.meta.activeTag(old_node)) = undefined;
inline for (std.meta.fields(@TypeOf(old_node))) |field| {
@field(updated_node, field.name) = @field(old_node, field.name);
}
if (!@hasField(@TypeOf(old_node), field_name)) {
return error.InvalidField;
} else if (@TypeOf(value) != @TypeOf(@field(old_node, field_name))) {
return error.InvalidArgument;
}
@field(updated_node, field_name) = value;
self.nodes.append(updated_node);
return error.NodeDoesNotExist;
}
I first thought it was an error on my side but, it just did not go away. I looked into the function throwing the error and it did not have any comptime functions except maybe @TypeOf()
.
The std.meta.activeTag() function code:
pub fn activeTag(u: anytype) Tag(@TypeOf(u)) {
const T = @TypeOf(u);
return @as(Tag(T), u);
}
If additional context is required your can either goto the full project on codeberg or jump directly into the file graph.zig