zig icon indicating copy to clipboard operation
zig copied to clipboard

Does `std.BoundedArray` really need `constSlice`?

Open wooster0 opened this issue 8 months ago • 3 comments

std.BoundedArray seems to be the only exception in the standard library that has an extra function constSlice() as an alternative to slice() to return a constant slice []const Type instead of []Type instead.

My proposal is to remove constSlice() and rename slice() to items() to be more similar to the other list types, such as the items field of std.ArrayList or the items() function of std.MultiArrayList. If std.BoundedArray has both of those why don't std.ArrayList and std.MultiArrayList etc. additionally have constItems or constSlice?

I suppose the pattern is that you're supposed to use constSlice wherever possible and slice only if needed to prevent accidental mutations. But I think the language already does enough here that this is not really needed? At least I've never been bitten with just using slice() everywhere.

wooster0 avatar May 28 '25 00:05 wooster0

It seems that the original utility of having separate slice() and constSlice() methods was that you were not able to call slice() on a const reference to a std.BoundedArray. Ever since commit f19b5ecf4b99652cd385cfdfedfa826260174871 that is indeed not the case and constSlice() seems to be superfluous.

Spiffyk avatar May 28 '25 15:05 Spiffyk

SegmentedList.at() solves this problem by accepting self: anytype and returns either const or mut ptr depending on whether self is a const or mut ptr. Other than using anytype, maybe this is a nice solution where users don't have to choose between 2 methods?

EDIT:

Ever since commit f19b5ec that is indeed not the case and constSlice() seems to be superfluous.

Oh I see thats also being done in BoundedArray now. Missed that.

travisstaloch avatar May 28 '25 21:05 travisstaloch

Guess there’s no problem implementing this proposal then. That would motivate me enough to make a PR but I’m just not sure if std.BoundedArray isn’t going to be removed from the standard library anyway. Guess that’s a different issue though.

ghost avatar May 29 '25 01:05 ghost

why would BoundedArray be removed ? it's quite useful in practice. My main complaint with it is that it's API is a bit foreign to ArrayList.

gwenzek avatar Jun 03 '25 08:06 gwenzek

why would BoundedArray be removed ? it's quite useful in practice. My main complaint with it is that it's API is a bit foreign to ArrayList.

Is the idea perhaps to move people to use the std.ArrayList.initBuffer() api?

travisstaloch avatar Jun 03 '25 10:06 travisstaloch

Yes, I think so. I don’t like it though because ArrayList still has an additional unnecessary capacity: usize field that BoundedArray doesn’t have and the initBuffer API is more cumbersome to use. I wish the language had something inbuilt for an array that carries a length. It would make stack allocation much easier. I don’t know why it’s made so difficult.

ghost avatar Jun 04 '25 01:06 ghost