I have this tagged union:
pub const Lex = union(enum) {
EOF,
IDENT: []const u8,
LPAREN,
RPAREN,
pub fn toString(allocator: std.mem.Allocator, self: *Lex) ![]const u8 {
return switch (self.*) {
.LPAREN => try std.fmt.allocPrint(allocator, "L_PAREN ( null", .{}),
// .IDENT => |val| try std.fmt.allocPrint(allocator, "IDENT {s}", .{val}),
.RPAREN => try std.fmt.allocPrint(allocator, "L_RAREN ) null", .{}),
.EOF => try std.fmt.allocPrint(allocator, "EOF null", .{}),
else => try std.fmt.allocPrint(allocator, "UNKNOWN_LEX", .{}),
};
}
};
but when Iâm trying to call it getting an error:
const tokens: ArrayList(Lex) = try token.tokenize(std.heap.page_allocator, file_contents);
for (tokens.items) |tkn| {
try stdout.print("{s}\n", .{tkn.toString(std.heap.page_allocator)});
}
defer tokens.deinit();
Error:
src/main.zig:28:44: error: no field or member function named 'toString' in 'token.Lex'
try stdout.print("{s}\n", .{tkn.toString(std.heap.page_allocator)});
~~~^~~~~~~~~
src/token.zig:5:17: note: union declared here
pub const Lex = union(enum) {
^~~~~
src/token.zig:11:9: note: 'toString' is not a member function
pub fn toString(allocator: std.mem.Allocator, self: *Lex) ![]const u8 {