Follow

Alright, it's day 3 of Advent of Code and I'm about to throw Zig out the window. Its basic behavior around pointers is stupendously underspecified and its compiler errors are terrible.

As an example, if you do `var a = &std.ArrayList(u32).init(al)`, what type would you expect a to have? If you said "pointer to ArrayList", wrong! It's a pointer to const ArrayList. Why is it const? I have no idea.

· · Web · 1 · 1 · 2

Similarly, eventually I realized that a problem I was having was that my ArrayLists were being allocated on the stack. Okay, I can sort of see how that makes sense, even though you have to hand them an allocator. Not that the error messages helped.

Can you allocate an ArrayList on the heap via something like `new std.ArrayList(u32)(...)`? Of course not. As far as I can tell, the best you can do is

var ptr = try alloc.create(std.ArrayList(u32));
ptr.* = std.ArrayList(u32).init(alloc);

There are a few things in the language that are really nice so far, but a whole lot of things where the language just doesn't tell you you've done something wrong until a totally different part of your code explodes with a cryptic error message.

Sign in to participate in the conversation
Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!