Skip to content

Instantly share code, notes, and snippets.

@kbd

kbd/funcs.zig Secret

Created January 2, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbd/83193ef65e8d3416e20a111b4a4d9624 to your computer and use it in GitHub Desktop.
Save kbd/83193ef65e8d3416e20a111b4a4d9624 to your computer and use it in GitHub Desktop.
const mymain = @import("main.zig");
var A = mymain.dummy_allocator;
const funcs = @import("funcs.zig");
pub var dummy_allocator: u8 = undefined;
fn buildFuncMap() void {
const info = @typeInfo(funcs);
const decls = info.Struct.decls;
inline for (decls) |d| {}
}
pub fn main() !void {
dummy_allocator = 1;
buildFuncMap();
}
@kbd
Copy link
Author

kbd commented Jan 2, 2021

$ zig build-exe main.zig
./funcs.zig:2:15: error: cannot store runtime value in compile time variable
var A = mymain.dummy_allocator;
              ^
./funcs.zig:2:1: note: referenced here
var A = mymain.dummy_allocator;
^
./main.zig:8:12: note: referenced here
    inline for (decls) |d| {}

@mikdusan
Copy link

mikdusan commented Jan 2, 2021

here's a stab at exporting a main config that can be used by another module:

funcs.zig

usingnamespace @import("root").config;

const std = @import("std");

pub fn doit() void {
    std.debug.warn("(from funcs) allocator: {*}\n", .{allocator});
}

main.zig

const funcs = @import("funcs.zig");
const std = @import("std");

fn buildFuncMap() void {
    const info = @typeInfo(funcs);
    const decls = info.Struct.decls;
    inline for (decls) |d| {}
}

pub fn main() !void {
    config.allocator = std.heap.c_allocator;
    std.debug.warn("(from main) config.allocator: {*}\n", .{config.allocator});

    buildFuncMap();
    funcs.doit();
}

pub const config = struct {
    pub var allocator: *std.mem.Allocator = undefined;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment