zig icon indicating copy to clipboard operation
zig copied to clipboard

Add a method to return length of ArrayList

Open mjoud opened this issue 5 years ago • 5 comments

Previously there was ArrayList.count() (removed in c3d8b1f) and then ArrayList.len (removed in dd570db). ArrayList.items.len is now the way to determine the number of items . I'd prefer a count() method; it could simply return items.len just like the previously removed count().

mjoud avatar Jun 22 '20 13:06 mjoud

I think that would violate the "only one obvious way to do things" point of the zig zen. I personally don't see what's wrong with using items.len, having a dedicated method to access this value seems like a pointless abstraction.

ifreund avatar Jun 22 '20 13:06 ifreund

I'm perfectly fine with that but this must eventually be properly documented. Knowing that ArrayList has an internal slice called items is not obvious unless you read the source.

Edit: To further clarify, it could be viewed as a leaking implementation detail and requires a stable implementation.

mjoud avatar Jun 22 '20 13:06 mjoud

items is part of the API. It is not an internal implementation detail.

andrewrk avatar Jun 22 '20 21:06 andrewrk

From the perspective of a new user trying to find out what the length of an array list is, this issue is how I discovered that .items.len is the recommended approach, that suggests the path to discovery is not ideal, at least for me.

It is not clear which fields are a stable part of a type's API vs an implementation detail. Both types of fields look the same. Maybe there's something we can do to make that clearer.

leighmcculloch avatar Dec 12 '22 16:12 leighmcculloch

items is part of the API. It is not an internal implementation detail.

Has it been removed from the documentation? It's not mentioned on std.ArrayList or std.array_list.ArrayList, and std.array_list.ArrayListAligned only mentions that it's a "wrapper around an array of T values aligned to alignment-byte addresses". The only places it's actually mentioned are in the function descriptions for shrinkRetainingCapacity and unusedCapacitySlice. It's possible to look at the source code and see it, but that's even less beginner friendly than before.

In the zig docs it's mentioned under Generic Data Structures, but that's not super searchable.

ITR13 avatar May 12 '24 16:05 ITR13