- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2.9k

Description
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.
Activity
Spiffyk commentedon May 28, 2025
It seems that the original utility of having separate
slice()
andconstSlice()
methods was that you were not able to callslice()
on aconst
reference to astd.BoundedArray
. Ever since commit f19b5ec that is indeed not the case andconstSlice()
seems to be superfluous.travisstaloch commentedon May 28, 2025
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:
Oh I see thats also being done in BoundedArray now. Missed that.
.slice()
and.constSlice()
fromstd.BoundedArray
in lieu of.items()
#24007ghost commentedon May 29, 2025
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.gwenzek commentedon Jun 3, 2025
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.
travisstaloch commentedon Jun 3, 2025
Is the idea perhaps to move people to use the std.ArrayList.initBuffer() api?
ghost commentedon Jun 4, 2025
Yes, I think so.
I don’t like it though because
ArrayList
still has an additional unnecessarycapacity: usize
field thatBoundedArray
doesn’t have and theinitBuffer
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.