#error: unable to resolve comptime value for struct method

1 messages · Page 1 of 1 (latest)

devout jungle
#

I'm working on something like this:

// main.zig
    const app = try App.init();
    defer app.deinit();

    const v1 = App.Group{ .base_path = "/v1" };
    _ = v1.get("/user", hello)
        .get("/member", hello);
    app.group(v1);
    ...

// app.zig
pub const App = struct {
    ...

    const Method = enum { Get, Put };
    const ListType = struct { Method, [:0]const u8, MethodHandler };
    pub const Group = struct {
        comptime list: []ListType = &[0]ListType{},
        base_path: []const u8,

        pub fn get(self: *const Group, pattern: []const u8, handler: MethodHandler) *const Group {
            const path = self.base_path ++ pattern;
            self.list = self.list ++ .{ .Get, path, handler };
            return self;
        }
    };
...
}

but:

zig build -freference-trace run
run
└─ run zuws
   └─ zig build-exe zuws Debug native 1 errors
src/app.zig:184:30: error: unable to resolve comptime value
            const path = self.base_path ++ pattern;
                         ~~~~^~~~~~~~~~
src/app.zig:184:30: note: slice value being concatenated must be comptime-known
referenced by:
    main: src/main.zig:15:9
    callMain: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:524:32
    callMainWithArgs: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:482:12
    main: /opt/homebrew/Cellar/zig/0.13.0/lib/zig/std/start.zig:497:12
...
error: the following build command failed with exit code 1:

Is there any way to keep this comptime in some way?

last mason
#

i think u need to do comptime base_path: []const u8, and the same with pattern

#

alternitavely, if u want this function to work at runtime , you cant use ++

devout jungle
devout jungle
last mason
#

ah

#

maybe make get take comptime self: *const Group?

#

i'm not entirely sure

devout jungle
#

That seems to have gotten me further along, in some way, having done what you suggested and thus having to wrap the Group declaration in a comptime block:

    comptime {
        const v1 = App.Group{ .base_path = "/v1" };
        _ = v1.get("/user", hello)
            .get("/member", hello);
        app.group(v1);
    }

I now get:

src/app.zig:185:17: error: cannot assign to constant
            self.list = self.list ++ .{ListType{ .Get, path, handler }};
            ~~~~^~~~~
src/main.zig:15:19: note: called from here
        _ = v1.get("/user", hello)
            ~~~~~~^~~~~~~~~~~~~~~~
#

might it be tied with the comptime declaration of list in the Group struct?

#

doing the same operation as with the base_path would however completely defeat the purpose of having a helper method

exotic panther
#

I was unaware that you could pass the struct pointer as a comptime param 🤔
Would this perhaps work with inline to force a function to always be comptime?

devout jungle
#

After several attempts (and one bug report later: https://github.com/ziglang/zig/issues/22583)

we've reached:

pub fn get(comptime self: *Group, pattern: []const u8, handler: MethodHandler) *Group {
            const path = self.base_path ++ pattern;
            const temp: ListType = .{
                .method = .Get,
                .base = path,
                .handler = handler,
            };
            self.list = self.list ++ .{temp};
            return self;
        }

However we still find that:

src/app.zig:194:35: error: expected type '[]app.App.ListType', found '*const [1]app.App.ListType'
            self.list = self.list ++ .{temp};
                        ~~~~~~~~~~^~~~~~~~~~
src/app.zig:194:35: note: cast discards const qualifier
src/main.zig:15:19: note: called from here
        _ = v1.get("/user", hello)
            ~~~~~~^~~~~~~~~~~~~~~~

which is better than what we had before, but still quite confusing as we've tried a lot of different things and none have come up as correct

GitHub

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software. - Issues · ziglang/zig

lethal estuary
#

you might have to put group into a comptime scope as a var and then pass it out as const tho. i have a comptime list that behaves kinda similarly to this