Hi All, I was trying to compose some structs with unions and ArrayList and hitting a dependency loop:
const std = @import("std");
var raw = std.heap.GeneralPurposeAllocator(.{}){};
pub const ALLOCATOR:std.mem.Allocator = raw.allocator();
pub const Value = struct {
value: i32 = 8,
};
pub const Sequence = struct {
children: std.ArrayList(Container) = std.ArrayList(Container).init(ALLOCATOR),
};
pub const Stack = struct {
children: std.ArrayList(Container) = std.ArrayList(Container).init(ALLOCATOR),
};
pub const Container = union(enum) {
val: Value,
sta: Stack,
seq: Sequence,
};
test "arraylist_union" {
var st = Stack{};
var sq = Sequence{};
const v = Value{ .value = 12 };
try sq.children.append(.{ .val = v });
try st.children.append(.{ .seq = sq });
try std.testing.expectEqual(
v.value,
st.children.items[0].seq.children.items[0].val.value
);
}
gives me the error:
.../zig/zig-macos-aarch64-0.10.1/lib/std/array_list.zig:51:13: error: dependency loop detected
pub fn init(allocator: Allocator) Self {
~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
Sequence: .../union_with_arraylist.zig:10:22
Container: .../union_with_arraylist.zig:21:10
Container: .../union_with_arraylist.zig:18:23
Sequence: .../union_with_arraylist.zig:11:56
Is the problem here the initialization in the struct? Or the composition?