New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: slices: add At function #65530
Comments
Addressing the goals in order:
I'm not convinced. |
Thank you for your feedback.
|
This could also be a comma syntax for |
See #53510 (comment). |
This function makes it worse, because a -1 off-by-one error silently returns the incorrect (last) value instead of failing promptly. |
This sounds like something that would be better implemented first outside of the standard library, and we can see whether people find it useful. |
@fzipp that is why the idea of negative indexes is optional. |
At
function
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Proposal Details
I propose to add a function
At
in slices package, which would would return the value at a specific index of a slice, but with some caveats. See belowFunction signature
Example
Why
1.
slices.At
would never panicIt is easy miss a bound check or to do it incorrectly
2.
slices.At
could receive negative indexes (Optional)Similarly to how Python works, or how JavaScript
Array.prototype.at
method works, passing a negative index gets an element in a backward fashion.This is useful for safely getting the last item of the array or penultimate.
I don't think this is critical to the proposal. If it is somewhat polemic, we can ignore this part and treat negative indexes as an error.
3.
slices.At
would make bound checks more explicitBefore:
After:
The text was updated successfully, but these errors were encountered: