I'm learning Zig, and it's looking great so far. There are some wrinkles I'd like to see fixed though, even if they're quite minor in the grand scheme

Zig wrinkles - DEV Community

submited by
Style Pass
2021-08-09 00:30:04

I'm learning Zig, and it's looking great so far. There are some wrinkles I'd like to see fixed though, even if they're quite minor in the grand schemes of things. That said, I've only included items that at least some others have agreed is something of an issue.

When init returns, a copy is made. Zig currently (0.7.x) guarantee copy elision only when struct literals are returned directly (thanks to ifreund for pointing this out). So essentially &instance.arena.allocator now points to a temporary, leading to memory corruption.

One solution is to make an instance method (passing self), but that makes the call site ugly. In my particular footgun case, it was solved by moving to ArrayListUnmanaged, where the allocator is passed in to every list operation. That makes it possible to remove the problematic init(&instance.arena.allocator)

Long term solutions are discussed in https://github.com/ziglang/zig/issues/591 and https://github.com/ziglang/zig/issues/7769

Leave a Comment