So I’m trying to build a scanner and I’m getting this error where the compiler is unable to find the init
method in the scanner
struct.
scanner.zig:
const Scanner = struct {
const Self = @This();
var start: usize = 0;
var current: usize = 0;
var line: usize = 1;
var source: []u8 = undefined;
var tokens: std.ArrayList(Token.Token) = undefined;
var keywords: std.StringArrayHashMap(Token.Token_Type) = undefined;
pub fn init(self: *Self, allocator: *std.mem.Allocator, src: []const u8) void {
self.source = src;
self.tokens = std.ArrayList(Token.Token).init(allocator);
self.keywords.init(allocator);
self.keywords.put("and", Token.Token_Type.AND);
...
}
Here’s my function in main where I’m trying to call it:
fn run(source: []const u8) !void {
const stdout = std.io.getStdOut().writer();
var scanner = Scanner{};
try scanner.init(allocator, source);
defer scanner.deinit();
var tokens = try scanner.scan_tokens();
defer tokens.deinit();
for (tokens.toOwnedSlice()) |token| {
const token_str = try token.to_string(allocator);
defer allocator.free(token_str);
try stdout.print("{s}\n", .{token_str});
}
}
I’ve also tried using &allocator
since the argument is a pointer, but that doesn’t seem to work either. I’ve looked at the different methods for ArrayList
and StringArrayHashmap
and they don’t possibly return an error so I’m pretty sure it’s not an issue with a missing try
and/or !
Edit: It actually looks like using put
does possibly return an error, so I put a try
next to them. However I am still producing the error.