Closed buzmeg closed 1 month ago
std.BoundedArray
is a userspace type in the standard library; Zig does not permit such types to be inititalized with array initialization syntax. Use its fromSlice
method instead:
const SomeBa = std.BoundedArray(i32, 4);
const ba: SomeBa = try .fromSlice(&.{ 0, 1, 2, 3 });
// equivalently
const ba = try SomeBa.fromSlice(&.{ 0, 1, 2, 3 });
That first form doesn't seem to work if in a struct but the second form does seem to work like so:
const std = @import("std");
pub fn main() !void {
const BA = std.BoundedArray(i32, 4);
const StructBA = struct {
aa: i32,
bb: BA,
};
const sba: [1]StructBA = .{
.{
.aa = 42,
.bb = try BA.fromSlice(&.{0, 1, 2, 3}),
},
};
std.debug.print("SS: {any}\n", .{sba, });
}
Thanks.
That first form doesn't seem to work if in a struct
Replacing the relevant line with the following works fine for me:
.bb = try .fromSlice(&.{ 0, 1, 2, 3 }),
You're on a slightly older Zig build, it's possible there was a bug with decl literals (the feature allowing this shorthand) in that version.
Zig Version
0.14.0-dev.1410+13da34955
Steps to Reproduce and Observed Behavior
The following code fails with "example.zig:8:26: error: type 'bounded_array.BoundedArrayAligned(i32,4,4)' does not support array initialization syntax":
Expected Behavior
Expect both declarations to work