Confusion about "unable to resolve comptime value"

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);
    }
}

I think I fixed it. I realized @Typeof returns []u8 instead of u8. I got lost in the error message. All good

Note about line return &[_]T{};. You can just write return &.{}; the compiler will infer the type.

1 Like

Good to know, ty very much!