1
0
mirror of https://github.com/golang/go synced 2024-11-26 21:11:57 -07:00

spec: clarify re-use of underlying arrays in slice operations

Please note the slight rewording for append: The spec now
requires that append reuses the underlying array if it is
sufficiently large. Per majority sentiment.

This is technically a language change but the current
implementation always worked this way.

Fixes #5818.
Fixes #5180.

R=rsc, iant, r, ken, minux.ma, dan.kortschak, rogpeppe, go.peter.90
CC=golang-dev
https://golang.org/cl/14419054
This commit is contained in:
Robert Griesemer 2013-10-16 16:16:54 -07:00
parent 37db880469
commit 15da997c7e

View File

@ -1,6 +1,6 @@
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of Oct 7, 2013", "Subtitle": "Version of Oct 16, 2013",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
@ -839,7 +839,7 @@ multi-dimensional types.
<h3 id="Slice_types">Slice types</h3> <h3 id="Slice_types">Slice types</h3>
<p> <p>
A slice is a descriptor for a contiguous segment of an array and A slice is a descriptor for a contiguous segment of an <i>underlying array</i> and
provides access to a numbered sequence of elements from that array. provides access to a numbered sequence of elements from that array.
A slice type denotes the set of all slices of arrays of its element type. A slice type denotes the set of all slices of arrays of its element type.
The value of an uninitialized slice is <code>nil</code>. The value of an uninitialized slice is <code>nil</code>.
@ -879,17 +879,9 @@ A new, initialized slice value for a given element type <code>T</code> is
made using the built-in function made using the built-in function
<a href="#Making_slices_maps_and_channels"><code>make</code></a>, <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
which takes a slice type which takes a slice type
and parameters specifying the length and optionally the capacity: and parameters specifying the length and optionally the capacity.
</p> A slice created with <code>make</code> always allocates a new, hidden array
to which the returned slice value refers. That is, executing
<pre>
make([]T, length)
make([]T, length, capacity)
</pre>
<p>
A call to <code>make</code> allocates a new, hidden array to which the returned
slice value refers. That is, executing
</p> </p>
<pre> <pre>
@ -897,8 +889,8 @@ make([]T, length, capacity)
</pre> </pre>
<p> <p>
produces the same slice as allocating an array and slicing it, so these two examples produces the same slice as allocating an array and <a href="#Slice_expressions">slicing</a>
result in the same slice: it, so these two expressions are equivalent:
</p> </p>
<pre> <pre>
@ -910,8 +902,8 @@ new([100]int)[0:50]
Like arrays, slices are always one-dimensional but may be composed to construct Like arrays, slices are always one-dimensional but may be composed to construct
higher-dimensional objects. higher-dimensional objects.
With arrays of arrays, the inner arrays are, by construction, always the same length; With arrays of arrays, the inner arrays are, by construction, always the same length;
however with slices of slices (or arrays of slices), the lengths may vary dynamically. however with slices of slices (or arrays of slices), the inner lengths may vary dynamically.
Moreover, the inner slices must be allocated individually (with <code>make</code>). Moreover, the inner slices must be initialized individually.
</p> </p>
<h3 id="Struct_types">Struct types</h3> <h3 id="Struct_types">Struct types</h3>
@ -2707,7 +2699,8 @@ and the result of the slice operation is a slice with the same element type as t
<p> <p>
If the sliced operand of a valid slice expression is a <code>nil</code> slice, the result If the sliced operand of a valid slice expression is a <code>nil</code> slice, the result
is a <code>nil</code> slice. is a <code>nil</code> slice. Otherwise, the result shares its underlying array with the
operand.
</p> </p>
<h4>Full slice expressions</h4> <h4>Full slice expressions</h4>
@ -5361,9 +5354,9 @@ append(s S, x ...T) S // T is the element type of S
<p> <p>
If the capacity of <code>s</code> is not large enough to fit the additional If the capacity of <code>s</code> is not large enough to fit the additional
values, <code>append</code> allocates a new, sufficiently large slice that fits values, <code>append</code> allocates a new, sufficiently large underlying
both the existing slice elements and the additional values. Thus, the returned array that fits both the existing slice elements and the additional values.
slice may refer to a different underlying array. Otherwise, <code>append</code> re-uses the underlying array.
</p> </p>
<pre> <pre>