-1

I have the below code in order to embed a set of files:

const std = @import("std");
pub const comptime_file_paths = [_][]const u8{
    "www/x/in.txt",
    "www/emd.txt",
    "www/e2.txt",
};

const EmbeddedFile = struct {
    path: []const u8,
    content: []const u8,
};

pub var embedded_files1 = std.ArrayList(EmbeddedFile).init(std.heap.ArenaAllocator);
pub var embedded_files2 = std.StringHashMap([]const u8).init(std.heap.ArenaAllocator);

pub fn main() !void {
    for (comptime_file_paths) |path| {
        const content = @embedFile(path);
        try embedded_files1.append(EmbeddedFile{ .path = path, .content = content });
        embedded_files2.put(path, content);
    }
}

But I got the error:

❯ zig run files.zig
files.zig:18:36: error: unable to resolve comptime value
        const content = @embedFile(path);
                                   ^~~~
files.zig:18:36: note: file path name must be comptime-known
referenced by:
    callMain: /usr/lib/zig/std/start.zig:614:32
    initEventLoopAndCallMain: /usr/lib/zig/std/start.zig:548:51
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

1 Answers1

1

You can use inline for to make this work:

    inline for (comptime_file_paths) |path| {
        const content = @embedFile(path);
        try embedded_files1.append(EmbeddedFile{ .path = path, .content = content });
        try embedded_files2.put(path, content);
    }
sigod
  • 3,514
  • 2
  • 21
  • 44