Skip to content

Commit a766277

Browse files
committed
spec: clarify value passed for final parameter of variadic functions
NOT A LANGUAGE CHANGE. Fixes #7073. LGTM=r R=r, rsc, iant, ken CC=golang-codereviews https://golang.org/cl/68840045
1 parent 8ca3372 commit a766277

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

doc/go_spec.html

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,27 +2915,32 @@ <h3 id="Calls">Calls</h3>
29152915
<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
29162916

29172917
<p>
2918-
If <code>f</code> is variadic with final parameter type <code>...T</code>,
2919-
then within the function the argument is equivalent to a parameter of type
2920-
<code>[]T</code>. At each call of <code>f</code>, the argument
2921-
passed to the final parameter is
2922-
a new slice of type <code>[]T</code> whose successive elements are
2923-
the actual arguments, which all must be <a href="#Assignability">assignable</a>
2924-
to the type <code>T</code>. The length of the slice is therefore the number of
2925-
arguments bound to the final parameter and may differ for each call site.
2918+
If <code>f</code> is <a href="#Function_types">variadic</a> with a final
2919+
parameter <code>p</code> of type <code>...T</code>, then within <code>f</code>
2920+
the type of <code>p</code> is equivalent to type <code>[]T</code>.
2921+
If <code>f</code> is invoked with no actual arguments for <code>p</code>,
2922+
the value passed to <code>p</code> is <code>nil</code>.
2923+
Otherwise, the value passed is a new slice
2924+
of type <code>[]T</code> with a new underlying array whose successive elements
2925+
are the actual arguments, which all must be <a href="#Assignability">assignable</a>
2926+
to <code>T</code>. The length and capacity of the slice is therefore
2927+
the number of arguments bound to <code>p</code> and may differ for each
2928+
call site.
29262929
</p>
29272930

29282931
<p>
2929-
Given the function and call
2932+
Given the function and calls
29302933
</p>
29312934
<pre>
29322935
func Greeting(prefix string, who ...string)
2936+
Greeting("nobody")
29332937
Greeting("hello:", "Joe", "Anna", "Eileen")
29342938
</pre>
29352939

29362940
<p>
29372941
within <code>Greeting</code>, <code>who</code> will have the value
2938-
<code>[]string{"Joe", "Anna", "Eileen"}</code>
2942+
<code>nil</code> in the first call, and
2943+
<code>[]string{"Joe", "Anna", "Eileen"}</code> in the second.
29392944
</p>
29402945

29412946
<p>

0 commit comments

Comments
 (0)