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
This is an alternative proposal for my another one: #23905
Short description of the problem
Sometimes, we may want to merge several slices (with the same element type).
The current implementation is like:
func merge(x, y, z []T) []T {
// this make line will reset all just allocated elements.
s := make([]T, 0, len(x)+len(y) + len(z))
s = append(s, x..)
s = append(s, y...)
s = append(s, z...)
return s
}
There are two problems in the implementation:
it is verbose.
resetting all just allocated elements by the make call is unnecessary.
My original proposal thread (#23905) is closed for three possible other solutions:
Bonus 2 (again, please ignore this bonus if you don't like it, I mean don't reject this main proposal by disliking the bonus).
The requirements for aSliceN may be relaxed to their elements must be assignable to type T, so that we can convert a []string slice to []interface{} slice (which is requested by many gophers) easily.
At this point in the language development I would personally like to see what can be done with generics before starting to add this kind of specific functionality to the language. Maybe you're right that generics won't be as good, but let's try it first.
ianlancetaylor
changed the title
proposal: Go 2: enhance "make" function
proposal: Go 2: permit builtin make function to initialize returned slice
Dec 18, 2018
This is an alternative proposal for my another one: #23905
Short description of the problem
Sometimes, we may want to merge several slices (with the same element type).
The current implementation is like:
There are two problems in the implementation:
make
call is unnecessary.My original proposal thread (#23905) is closed for three possible other solutions:
However, I think the three solutions are hard to be implemented perfectly or not very practical.
So here I propose another alternative solution.
The solution: enhance the built-in
make
functionI propose to let
make
calls support the following manner:where each
gapN
is anint
value, eachaSliceN
is a slice with element type ofT
.Only the elements at the gap segments will be zeroed.
All
gapN
can be omitted. So the following call is also valid:Omitted gaps are viewed as zero.
I think the alternative solution is both efficient and much less verbose.
============================================
Bonus for proposal (please ignore this bonus if you don't like it, I mean don't reject this main proposal by disliking the bonus):
The text was updated successfully, but these errors were encountered: