How to init StaticStringMap with allocator?

I’m getting some errors that I don’t understand.

Here is the simplified code:

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    const ssm = try std.StaticStringMap([]const u8).init(.{
        .{ "a", "b" },
    }, allocator);
    ssm.deinit(allocator);
}

This results in this error:

/zig/lib/std/static_string_map.zig:161:18: error: unable to resolve comptime value
            for (kvs_list, 0..) |kv, i| {
                 ^~~~~~~~
/zig/lib/std/static_string_map.zig:161:18: note: tuple field access index must be comptime-known
referenced by:
    init__anon_3541: /zig/lib/std/static_string_map.zig:118:31
    main: src/main.zig:8:49
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I’m using zig version: 0.14.0-dev.14+ec337051a

Any ideas?

Thanks!

Judging from tests you need to specify the type of init function’s kvs_list argument. In your case, it can be done like this:

const KV = struct { []const u8, []const u8 };
const ssm = try std.StaticStringMap([]const u8).init([_]KV{
...
2 Likes

Awesome! Thanks @tensorush

1 Like