Skip to content

Does std.BoundedArray really need constSlice? #24003

@ghost

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

added
standard libraryThis issue involves writing Zig code for the standard library.
on May 28, 2025
Spiffyk

Spiffyk commented on May 28, 2025

@Spiffyk

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 f19b5ec that is indeed not the case and constSlice() seems to be superfluous.

travisstaloch

travisstaloch commented on May 28, 2025

@travisstaloch
SponsorContributor

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.

ghost

ghost commented on May 29, 2025

@ghost

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

gwenzek commented on Jun 3, 2025

@gwenzek
SponsorContributor

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

travisstaloch commented on Jun 3, 2025

@travisstaloch
SponsorContributor

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?

ghost

ghost commented on Jun 4, 2025

@ghost

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @alexrp@andrewrk@travisstaloch@Spiffyk@gwenzek

      Issue actions

        Does `std.BoundedArray` really need `constSlice`? · Issue #24003 · ziglang/zig