You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Proposal Details
In #33359, iant@ commments in #33359 (comment):
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 tos[len(s)-1]
. As part of this equivalence, it will panic iflen(s)==0
.It may appear that
s[len(s)-1]
is shorter thanslices.Last(s)
, but this is only the case for very short slice names likes
. For more realistic slice names,slices.Last
will be shorter and will also avoid stuttering the slice's name.@neild @timothy-king
The text was updated successfully, but these errors were encountered: