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?