]> Cypherpunks repositories - gostls13.git/commitdiff
spec: clarify when range x does not evaluate x
authorRuss Cox <rsc@golang.org>
Fri, 15 Feb 2013 19:39:28 +0000 (14:39 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 15 Feb 2013 19:39:28 +0000 (14:39 -0500)
Fixes #4644.

R=r, adonovan
CC=golang-dev
https://golang.org/cl/7307083

doc/go_spec.html

index 5f84a296452ed8651327f0c52a5cd7e73224ba7a..66294c7a5ab78ef1cebecbc37ad51c93ce75d719 100644 (file)
@@ -4377,9 +4377,15 @@ the range clause is equivalent to the same clause with only the first variable p
 </p>
 
 <p>
-The range expression is evaluated once before beginning the loop
-except if the expression is an array, in which case, depending on
-the expression, it might not be evaluated (see below).
+The range expression is evaluated once before beginning the loop,
+with one exception. If the range expression is an array or a pointer to an array
+and only the first iteration value is present, only the range expression's
+length is evaluated; if that length is constant by definition
+(see ยง<a href="#Length_and_capacity">Length and capacity</a>),
+the range expression itself will not be evaluated.
+</p>
+
+<p>
 Function calls on the left are evaluated once per iteration.
 For each iteration, iteration values are produced as follows:
 </p>
@@ -4396,8 +4402,8 @@ channel         c  chan E, &lt;-chan E       element  e  E
 <ol>
 <li>
 For an array, pointer to array, or slice value <code>a</code>, the index iteration
-values are produced in increasing order, starting at element index 0. As a special
-case, if only the first iteration variable is present, the range loop produces
+values are produced in increasing order, starting at element index 0.
+If only the first iteration variable is present, the range loop produces
 iteration values from 0 up to <code>len(a)</code> and does not index into the array
 or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
 </li>