4 ms·
> Initializing arrays is weird in Zig. Lets say you want to have a 0 initialized array, you declare it like [_]u8{0} * 4 which means I want an array, of type u8
by AnIdiotOnTheNet 5y ago
> Initializing arrays is weird in Zig. Lets say you want to have a 0 initialized array, you declare it like [_]u8{0} * 4 which means I want an array, of type u8, that is initialized to 0 and is 4 elements long. You get used to the syntax, but it’s not intuitive.
Alternatively:
var some_array = std.mem.zeroes([4]u8);
Though as mentioned later in the article the standard library documentation is not very good, making this not as obvious as it could be.
> Everything in Zig is const x = blah;, so why are functions not const bar = function() {};?
Good question, there's an accepted proposal to fix this: https://github.com/ziglang/zig/issues/1717 https://github.com/ziglang/zig/issues/1717
> The builtin compiler macros (that start with @) are a bit confusing. Some of them have a leading uppercase, others a lowercase, and I never did work out any pattern to them.
In idiomatic zig, anything that returns a type is uppercased as though it were itself a type. Since only a few builtins return types, the vast majority of builtins will start with a lowercase letter. I think it is only `@Type`, `@TypeOf`, `@This`, and `@Frame` that don't.
- rgrmrts 5y agoAny recommendations on learning more about what constitutes idiomatic zig? This is an issue I have with learning any new language - it’s kind of hard for me to figure out what writing idiomatic code in that language looks like. I usually go looking for popular/high quality projects and reading that code but it takes away from the experience of actually just toying around not to mention it being hard knowing what a high quality project is. Thanks in advance!
- gavinray 5y agohttps://ziglearn.org/ https://ziglearn.org/
- dnautics 5y agoI don't think of this as an idiomaticity guide.
- gavinray 5y agoIt's unfortunately the only comprehensive reference I know of besides the official docs. There is "Ziglings", but those are a collection of small exercises with answers rather than a full guide. https://github.com/ratfactor/ziglings https://github.com/ratfactor/ziglings If you know of better resources than these two, please do share (not being passive aggressive here).
- dnautics 5y agohonestly, the standard library. But there should be an idiomaticity guide. At least for capitalization patterns, any other naming conventions, etc (things that zig fmt can't capture). I don't know that this officially exists, anywhere yet. Here is an example of what I'm talking about in my $DAYJOB lang: https://hexdocs.pm/elixir/naming-conventions.html#content https://hexdocs.pm/elixir/naming-conventions.html#content edit: see sibling comment, apparently I never noticed the style guide in the standard docs
- deleted 5y ago[deleted]
- cturtle 5y agoThe style guide in the language reference explains the accepted naming conventions [0]. [0]: https://ziglang.org/documentation/master/#Style-Guide https://ziglang.org/documentation/master/#Style-Guide
- Shadonototra 5y agoif you need to import a package to clear an array, something went very wrong somewhere..
- christophilus 5y agoWhy?
- Shadonototra 5y agobecause it's trivial, it's like assigning a value to an integer, it shouldn't require a package
- messe 5y agoDepending on your perspective, it's not trivial. It's significantly more expensive than assigning a value to an integer. Zeroing a [4096]u64 would require several thousand times more operations than zeroing a u64. In the areas that zig targets, this can be quite important.
- Shadonototra 5y agoi disagree, it's just backward to need to import a package
- kristoff_it 5y agoIn Zig zero initialization is not idiomatic. Unless you have an active reason to do so (and during AoC you need zero init a lot more than normal IME), you should just set the array to undefined like so: var foo: [64]usize = undefined;
- ithkuil 5y agoI don't know zig. Is one a constant initializer while the other is not?
- anonymoushn 5y agoIt looks like the value returned by std.mem.zeroes will end up being a compile-time constant, but I'm only like 90% on this.