#Error: unable to resolve comptime value

1 messages · Page 1 of 1 (latest)

wary fulcrum
#

I'm trying to make some data structures that I potentially want to use in the future, but so far I'm having an issue with a very simple pooling mechanism.

const Libtests = @import("Libtests");
const pool = @import("pool.zig");
const Pool = pool.Pool;

pub var allocator: std.mem.Allocator = undefined;

const Data = struct {
    text: []u8,
    value: u32,
};

pub fn main() !void {
    var alp = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
    allocator = alp.allocator();
    var testPool = Pool(Data, allocator, 10);
    var data = testPool.Get();
    data.text = allocator.alloc(u8, 5);
    data.text[0] = 'T';
    data.text[1] = 'e';
    data.text[2] = 's';
    data.text[3] = 't';
    data.text[4] = '_';
    data.value = 123456;
    testPool.Return(data);
    data = testPool.Get();
    std.debug.print("DATA: {s} {d}", .{ data.text, data.value });
}```

```const std = @import("std");

pub fn Pool(comptime T: type, allocator: std.mem.Allocator, startingDepth: usize) type {
    return struct {
        const Self = @This();
        const alloc = allocator;
        var pool: []*T = alloc.alloc(*T, startingDepth);
        var poolIndex: usize = 0;

        pub fn Return(item: *T) void {
            if (pool.len <= poolIndex) {
                const tmp: []*T = pool;
                pool = alloc.alloc(*T, tmp.len * 2);
                @memcpy(pool[0..tmp.len], tmp);
                alloc.free(tmp);
            }
            pool[poolIndex] = item;
            poolIndex += 1;
        }

        pub fn Get() *T {
            if (poolIndex > 0) {
                poolIndex -= 1;
                return pool[poolIndex];
            } else {
                return &T{};
            }
        }
    };
}```

This results in:

```src/main.zig:16:31: error: unable to resolve comptime value
    var testPool = Pool(Data, allocator, 10);
                              ^~~~~~~~~```

But I don't understand why. All should be comptime available, no?
fallow mango
# wary fulcrum I'm trying to make some data structures that I potentially want to use in the fu...

allocator = alp.allocator() is code that executes at runtime, but then you try to call a comptime function with the value of allocator

instead, you probably want an init function inside the pool struct that accepts the allocator

also, you are using var pool: []*T = ...; inside the pool struct, but that makes a global variable, not a struct field. you probably want pool: []*T = ..., to make a struct field.

wary fulcrum
#

Allright, that makes a little more sense to me. You are also correct about not wanting to use globals. I changed it to only require the Data struct for the main function```pub fn Pool(comptime T: type) type {
return struct {
Self: type = @This(),
alloc: std.mem.Allocator,
pool: []*T,
poolIndex: usize = 0,

    pub fn Init(self: Pool, allocator: std.mem.Allocator, startingDepth: usize) void {
        self.alloc = allocator;
        self.pool = allocator.alloc(T, startingDepth);
    }``` but it still errors out:```src/main.zig:16:9: error: variable of type 'type' must be const or comptime
var testPool = Pool(Data);
    ^~~~~~~~

src/main.zig:16:9: note: types are not available at runtime```. Up to the line where the error occurs it still is the same in the main function. The error disappears if I switch it from a var to a const, but from what I understand that would then lock up all its fields to no longer be mutable, which defeats the purpose of the structure.
I'm trying to somewhat mimic the way the ArrayList is built, and can't see the probably obvious difference. What does the ArrayList do that I don't here?