-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: spec: accept slices and single elements in one append? #4096
Comments
Do I interpret correctly that under the new syntax these would be equivalent? xs = append(xs, a, b, cs..., d) xs = append(append(append(xs, a, b), cs...), d) And what would the following do? s := []int{0, 1, 2, 3, 4} s = append(s[0:2], s[1:3], s[2:4], s[3:5]) If it works the same way as above, it should be equivalent to: s = append(append(append(s[0:2], s[1:3]...), s[2:4]...), s[3:5]...) Which yields... well it should be obvious. To be honest I'm not sure if this will bring more simplicity or more confusion... (Though it will definitely bring more SliceTricks.) |
Maybe a simplification of the model could be:
|
This is a special case of #18605. |
With the upcoming generics proposal, it's almost possible to write append without relying on built-in "magic" (almost because we still have special handling for strings). I think we should not introduce more special cases, but instead remove them. It's undoubtedly useful to have the proposed more general append functionality, but the need doesn't arise all the often. I'm not convinced that the benefits outweighs the costs. |
@griesemer another “almost” is that the runtime picks a new slice capacity that is optimal for the memory allocator. (See somewhat related discussion in #24204 and its progenitor #24163.) |
A similar problem: #29186 |
As @griesemer notes above, this can be written using generics, although not necessarily in a fully type-safe way. With the current design draft it would look something like this: func Append(type Elem)(s []Elem, a ...interface{}) []Elem {
for _, v := range a {
switch v := v.(type) {
case Elem:
s = append(s, v)
case []Elem:
s = append(s, v...)
default:
panic("invalid type")
}
}
return s
} Therefore, we suggest that this not necessary to add to the language itself. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Now that we have significantly more experience with generics, I think we should reconsider this proposal (in the form given in @ugorji's comment). We now have a better handle on the problems that can be solved by generics, but shouldn't necessarily. @ianlancetaylor pointed out that this is a special case of #18605. That's true in some sense, but allowing more flexible ... arguments to functions raises subtle questions about slice aliasing and efficiency. Doing this just for The exact generic Append function that @ianlancetaylor gave in this comment has come back as a new proposal, so I think it makes sense to consider these two proposals together, as two different ways to solve the same problem. |
There is a tricky case when we allow appending slices which alias the slice being appended to. See #60138 for gory details about this in the context of
What happens if
Do we want the result to be Maybe we're happy with a result of |
This proposal is a language change, which has a higher bar than a library change. This change introduces a new syntax that is specific to
The new proposal is different, though. It just concatenates slices. The idea here is to concatenate either elements or slices. |
My sense is that we should make In general, while it's theoretically always possible to do this with only enough scratch space for one element, I think such a schedule can get very complicated (e.g., |
That's true. Nevertheless, this proposal and #56353 have significant overlap, so I still think we should consider them together. While this proposal would clearly be a language change, doing this in Today, people usually write this as several nested
To add to this, a generalized |
The text was updated successfully, but these errors were encountered: