-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
std.ArrayList: make unmanaged the default #24801
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
Conversation
lib/std/std.zig
Outdated
| pub const Uri = @import("Uri.zig"); | ||
|
|
||
| /// A contiguous, growable list of items in memory. This is a wrapper around a | ||
| /// slice of `T` values. Initialize with `init`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /// slice of `T` values. Initialize with `init`. | |
| /// slice of `T` values. |
nothing else needed as the lines below say how you should actually inititalize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also init just doesn't exist on ArrayList
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's why i made this suggestion?
|
Since its breaking change wouldn't it also make sense to remove default field values? |
|
Tested locally with |
|
why are we removing managed containers? what pattern do unmanaged containers allow that managed ones dont? |
|
It's the other way around. Having an extra field is more complicated than not having an extra field, so not having it is the null hypothesis. What pattern does having an allocator field allow that not having one doesn't?
But there are downsides:
The reasoning goes like this: the upsides are not worth the downsides. Also, given that the correct allocator is always handy, and incorrect use can be trivially safety-checked, the simplicity of only having one implementation is quite valuable compared to the convenience that is gained by having a second implementation. In practice, this has not been a controversial change with experienced Zig users. |
|
Are there any plans on changing the other std containers to fit this new pattern?
Apologies in advance if this has been answered in another PR/issue, my searches have turned up nothing. I can move this into its own issue if this would be more appropriately placed there. |
|
Yes, all of them. |
|
Hi all - I just tried out version 0.15.1 and wanted to share some late feedback on this change. As was mentioned in the discussion:
That may be true, but from my perspective, this shift pushes complexity onto the user, particularly newer users. The allocator is now required in every single call to ArrayList et al. (e.g., init, deinit, append, etc.), which increases verbosity and makes common patterns more cumbersome. While the complexity hasn't been removed, it's essentially been offloaded from the API to the user, which raises the barrier to entry in an area that’s quite central to Zig development. Don't think just about having a self-contained ArrayList inside a function - imagine passing an ArrayList between modules, while also needing to keep track of the allocator reference. In practice, users might end up writing their own wrapper around ArrayList just for convenience. At that point, why remove it from std in the first place? Maybe passing ArrayLists around is considered an anti-pattern in Zig. If so, it would help to make that guidance more explicit. I understand the rationale behind the decision, but I still think this change deserves more scrutiny given how fundamental these structs are. |
actually in that case, you are already passing an allocator (and if you dont, you probably should anyways so you could see "The Allocator" and the possibility of memory allocation) so there would be no api update pretty much and also passing ArrayList around should only be only limited to functions that may modify the ArrayList in "ArrayList ways", otherwise you'd be better of passing []T s around. |
As a nice little bonus, the namespaces no longer have any redundancies.
Upgrade Guide
std.ArrayList->std.array_list.Managedstd.ArrayListAligned->std.array_list.AlignedManaged