Skip to content
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 Last function #69520

Closed
eliben opened this issue Sep 18, 2024 · 2 comments
Closed

proposal: slices: add Last function #69520

eliben opened this issue Sep 18, 2024 · 2 comments
Labels
Milestone

Comments

@eliben
Copy link
Member

eliben commented Sep 18, 2024

Proposal Details

In #33359, iant@ commments in #33359 (comment):

The most common case here is probably len(s) - 1. It's worth noting that if we had generics, we could have library functions like slices.Last(s) and slices.Pop(s) that would work with any slice type. That might remove many of the cases where this is valuable.

We now have generics, and a slices package full of useful generic functions.

I propose adding slices.Last, that would return the last element of a slice. It would be equivalent to s[len(s)-1]. As part of this equivalence, it will panic if len(s)==0.

It may appear that s[len(s)-1] is shorter than slices.Last(s), but this is only the case for very short slice names like s. For more realistic slice names, slices.Last will be shorter and will also avoid stuttering the slice's name.

@neild @timothy-king

@gopherbot gopherbot added this to the Proposal milestone Sep 18, 2024
@gabyhelp
Copy link

@eliben
Copy link
Member Author

eliben commented Sep 18, 2024

Oops, closing as duplicate of #53510

Thanks, @gabyhelp

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants