So, I am very new to Zig, just started a few minutes ago and I decided to learn the language through tackling Crafting Interpreters (Bytecode VM). I am porting stuff to Zig, but I am not brilliant.
So,
pub fn writeChunk(self: *Chunk, byte: u8) void {
if (self.count >= self.capacity) {
const old_capacity = self.capacity;
self.capacity = common.growCapacity(old_capacity);
self.code = common.growArray(@TypeOf(self.code), self.allocator, self.code, self.capacity); // error: expected type '[][]u8', found '[]u8'
}
self.code[self.count].* = byte;
self.count += 1;
}
the error is:
self.code = common.growArray(@TypeOf(self.code), self.allocator, self.code, self.capacity); // error: expected type '[][]u8', found '[]u8'
But if I try to do []self.code I get: error: unable to resolve comptime value
growArray looks like:
pub fn growArray(comptime T: type, allocator: std.mem.Allocator, old_ptr: []T, new_capacity: usize) ![]T {
if (new_capacity == 0) {
allocator.free(old_ptr);
return &[_]T{};
}
if (old_ptr.len == 0) {
return allocator.alloc(T, new_capacity);
} else {
return allocator.realloc(old_ptr, new_capacity);
}
}