-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Syntax for array default initialization #4847
Comments
I'd suggest: var list: [128]TestData; to be functionally equivalent to this: var list: [128]TestData = undefined;
var i: usize = 0;
while (i < list.len) : (i += 1) {
list[i] = {};
} For primitive types, like Note that on master, When I first tried Zig, I was curious why this syntax was not allowed; I think it bares repeating here. |
After discussions on Discord, I'll try to summarise here. The consenus seemed like library functions would do well for this. // all fields are their zero-value
var list = mem.zeroes([128]TestData);
// initialize fields to default values declared in the struct;
// recurses fields that are structs; compile error if not all fields have defaults
var list = mem.defaults([128]TestData);
// fields with defaults are set, rest are zeroed, recurses fields that are structs
var list = mem.defaultsOrZeroes([128]TestData);
// same as defaultsOrZeroed, except undefined instead
var list = mem.defaultsOrUndefined([128]TestData); This is because of wanting to keep explicit initialization, and also how It's also because hidden code generation (i.e: any kind of initialization code being run should be explicit). This would make the buffer example fairly simple: var buf = mem.zeroes([4096]u8); A bit more long winded than ideal, but it does at least address the concern that I have with that example, and it's honestly not that bad, considering the wish to be explicit. But yes, the |
I like the idea of just using std.mem.zeroes. Only issue with it: Rewriting std.mem.zeroes in how it handles arrays may sidestep this problem. |
Before (when given an array with many elements): ``` zig\std\mem.zig:345:13: error: evaluation exceeded 1000 backwards branches for (array) |*element| { ^ ``` related to ziglang#4847 (comment)
@Sobeston Hmmm. That's unfortunate. Regardless though, I'm not sure why you shouldn't be able to loop 500 times. 🤔 |
@Tetralux there's currently a loop in std.mem.zeroes for arrays for each element. Comptime is deliberately limited when it comes to looping (you can think of the end of a loop going back to the start as a branch going backwards). My PR is a fix for this - no looping, just does it in one step. |
I feel like that error message is needlessly esoteric. I guess it's a generic message. Definitely confusing though; "I didn't write a backwards branch - I wrote a loop." To make that worse, I don't really see why it would have 1000 branches while looping 500 times; where's that factor of 2 coming from? |
(inside std.mem.zeroes) .Array => |info| {
var array: T = undefined;
for (array) |*element| {
element.* = zeroes(info.child);
}
return array;
}, Each iteration calls zeroes while already in zeroes, meaning another backwards branch. So there's your factor of 2. |
Before (when given an array with many elements): ``` zig\std\mem.zig:345:13: error: evaluation exceeded 1000 backwards branches for (array) |*element| { ^ ``` related to #4847 (comment)
So I wanted to create an static array holding structs that has default values. I looked through the docs and tried numerous things before I figured out how to do this. (Or rather, I got help on Discord)
Best solution I've found:
I think this however would be more intuitive:
And/Or this:
Sure, it's another way to do the same thing, which is generally not zig-like, but it's not really "new" syntax, just uses already existing ones.
It's cleaner and is something I would have arrived at "naturally" pretty much without having to look at the documentation.
The text was updated successfully, but these errors were encountered: