I just tried compiling a small variation of this example, in which I access the first two elements of array
via a loop:
const std = @import("std");
const print = std.debug.print;
const SelfRefStruct = struct {
data: u8,
next: ?*const @This(),
};
fn generateSelfRefData() [10]SelfRefStruct {
comptime var array: [10]SelfRefStruct = undefined;
array[0] = .{ .data = 'p', .next = &array[1] };
array[1] = .{ .data = 'a', .next = null };
return array;
}
pub fn main() void {
const fakeReffingData = comptime generateSelfRefData();
for (0..1) |i| {
print("{c}", .{fakeReffingData[i].data});
}
}
This code not only fails to compile with this loop, but the error message is basically opaque:
$ zig build run
run
+- run zig-play
+- zig build-exe zig-play Debug native failure
error: the following command exited with error code 3:
C:\tools\zig-dev\zig.exe build-exe -ODebug -Mroot=C:\Users\biosb\zig\zig-play\src\main.zig --cache-dir C:\Users\biosb\zig\zig-play\zig-cache --global-cache-dir C:\Users\biosb\AppData\Local\zig --name zig-play --listen=-
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
+- run zig-play transitive failure
+- zig build-exe zig-play Debug native failure
+- install transitive failure
+- install zig-play transitive failure
+- zig build-exe zig-play Debug native (reused)
error: the following build command failed with exit code 1:
C:\Users\biosb\zig\zig-play\zig-cache\o\074cf8b1e6d684493d443d6f6d3dbab6\build.exe C:\tools\zig-dev\zig.exe C:\Users\biosb\zig\zig-play C:\Users\biosb\zig\zig-play\zig-cache C:\Users\biosb\AppData\Local\zig --seed 0xe625a989 -Zad55c03e619be54f run
I’m still trying to find a solution to the dependency loop issue described here, and was intrigued by how this array was initialized at comptime
.
But correct me if I’m wrong, but I can’t really use this technique to initialize arrays declared at container-scope???