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: spec: accept slices and single elements in one append? #4096

Open
rsc opened this issue Sep 18, 2012 · 13 comments

Comments

@rsc
Copy link
Contributor

commented Sep 18, 2012

Ken points out that using append is harder than it might be.
Right now if you have var xs []string and want to append a, b, string, cs []string, and
d string, you have to do

xs = append(xs, a, b)
xs = append(xs, cs...)
xs = append(xs, d)

or

xs = append(append(append(xs, a, b), cs...), d)

The suggestion is to accept

xs = append(xs, a, b, c, d)

There is an ambiguity for slices of interface{}. If you have

var xs []interface{}
var ys []interface{}

xs = append(xs, ys)

is ambiguous: does it append a single interface{}(ys) or does it append each of the
elements in ys?
We would have to give this the same meaning it has today, meaning the former.

Thus the rules for append to a []T would be that it takes one or more elements z and for
each:
1. If z can be assigned to type T and is not 'z...', it is added as a single element.
2. Else if z is a slice of T, its elements are appended.
3. Else the compiler complains.

We would need to keep
xs = append(xs, ys...)
as a special form for resolving the ambiguity in the other direction when T =
interface{}, and for backwards compatibility.
@speter

This comment has been minimized.

Copy link

commented Sep 18, 2012

Comment 1:

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.)
@rsc

This comment has been minimized.

Copy link
Contributor Author

commented Sep 18, 2012

Comment 2:

This probably isn't the right place for a discussion. I just wanted to
record the idea.
@rsc

This comment has been minimized.

Copy link
Contributor Author

commented Dec 10, 2012

Comment 3:

Labels changed: added size-xl.

@robpike

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2013

Comment 4:

Labels changed: removed go1.1maybe.

@rsc

This comment has been minimized.

Copy link
Contributor Author

commented Nov 27, 2013

Comment 5:

Labels changed: added go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor Author

commented Dec 4, 2013

Comment 6:

Labels changed: added release-none, removed go1.3maybe.

@rsc

This comment has been minimized.

Copy link
Contributor Author

commented Dec 4, 2013

Comment 7:

Labels changed: added repo-main.

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Sep 10, 2014

Comment 8:

Labels changed: added languagechange.

@rsc rsc self-assigned this Sep 10, 2014

@rsc rsc added this to the Unplanned milestone Apr 10, 2015

@rsc rsc changed the title spec: accept slices and single elements in one append? proposal: spec: accept slices and single elements in one append? Jun 20, 2017

@rsc rsc added the Go2 label Jun 20, 2017

@ugorji

This comment has been minimized.

Copy link
Contributor

commented Dec 11, 2017

Maybe a simplification of the model could be:

a=append(a, b, c..., d, e, f...) means append b, all elements of c, d, e, and all elements of f
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented May 29, 2018

This is a special case of #18605.

@griesemer

This comment has been minimized.

Copy link
Contributor

commented Feb 5, 2019

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.

@josharian

This comment has been minimized.

Copy link
Contributor

commented Feb 6, 2019

@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.)

@dotaheor

This comment has been minimized.

Copy link

commented Feb 6, 2019

A similar problem: #29186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.