While exploring the Allocator interface, I came the lines
// The type erased pointer to the allocator implementation
ptr: *anyopaque,
vtable: *const VTable,
in the file that felt out of place. The seemed like file level globals, but without a var
or a const
declaration.
Turns out, files are compiled into structs.
As an example, this is how we can verify this.
cat module1.zig
a: u32,
b: u32,
cat module2.zig
const m1 = @import("module1.zig");
const std = @import("std");
pub fn main() !void {
var s = m1{ .a = 43, .b = 46 };
std.debug.print("{}\n", .{s.a});
}
Running module2.zig using zig run module2.zig
, I get :
43
Top comments (2)
This is also documented here: ziglang.org/documentation/0.9.1/#i...
Thanks, I did not notice this in the std. documentation ! Leaving this here anyway for someone if they are interested.