Skip to content

Conversation

Fri3dNstuff
Copy link
Contributor

The existing implementation needlessly special-cased removal of the last item, even though it is handled perfectly fine by the rest of the code.

Additionally:

  • added the documented but non-existent assert that "index is in bounds",
  • removed the documented assertion that "list is not empty", as that is already implied by the previous assertion.

If my usage of defer or orelse unreachable is confusing, I'll happily change them to a three-line solution, and .?.

The correctness of this simplification relies on the left-to-right evaluation order of the operands of assignment.

@Fri3dNstuff Fri3dNstuff requested a review from andrewrk October 9, 2025 06:14
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is now equivalent to the previous one, but it can still be better, by setting the invalidated element to undefined, helping to catch bugs, and sometimes even facilitate optimizations.

@Fri3dNstuff
Copy link
Contributor Author

I've added code to override the removed item with undefined, both in swapRemove and pop.

@Fri3dNstuff Fri3dNstuff requested a review from andrewrk October 10, 2025 05:11
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, thank you.

@andrewrk andrewrk merged commit 87c1894 into ziglang:master Oct 12, 2025
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants