I've the below in my code:
var routeRegister = std.StringHashMap(*const fn (response: *http.Server.Response) void).init(allocator);
try routes.init(&routeRegister);
defer routeRegister.deinit();
const some_data_info = @typeInfo(@TypeOf(routeRegister));
print("{}\n", .{some_data_info});
But I got the below error:
/usr/lib/zig/std/fmt.zig:662:22: error: values of type '[]const builtin.Type.StructField' must be comptime-known, but index value is runtime-known
for (value, 0..) |elem, i| {
^~~~~
/usr/lib/zig/std/builtin.zig:318:15: note: struct requires comptime because of this field
type: type,
^~~~
/usr/lib/zig/std/builtin.zig:318:15: note: types are not available at runtime
type: type,
^~~~
/usr/lib/zig/std/builtin.zig:321:20: note: struct requires comptime because of this field
alignment: comptime_int,
^~~~~~~~~~~~
I tried:
var routeRegister = comptime std.StringHashMap(*const fn (response: *http.Server.Response) void);
try routeRegister.init(allocator);
But got:
server.zig:99:13: error: variable of type 'type' must be const or comptime
var routeRegister = comptime std.StringHashMap(*const fn (response: *http.Server.Response) void);
^~~~~~~~~~~~~
server.zig:99:13: note: types are not available at runtime
The second error occurs because std.StringHashMap
returns a type. Just declare it constant (you can also declare it outside of any function):
const RouteRegister = std.StringHashMap(*const fn (response: *http.Server.Response) void);
var routeRegister = RouteRegister.init(allocator);
The first error occurs because @typeInfo
returns std.builtin.Type
, which cannot be formatted by the default formatter. Just remove @typeInfo
and print the type
directly:
print("{}\n", .{ @TypeOf(routeRegister) });
// or just
print("{}\n", .{ RouteRegister });