Commit ef45e64a authored by Robert Griesemer's avatar Robert Griesemer

- unifying rules for var decls, short var decls, and assignments

DELTA=39  (4 added, 15 deleted, 20 changed)
OCL=33639
CL=33649
parent 005509e3
...@@ -1547,41 +1547,41 @@ var ( ...@@ -1547,41 +1547,41 @@ var (
</pre> </pre>
<p> <p>
If there are expressions, their number must be equal If a list of expressions is given, the variables are initialized
to the number of identifiers, and the n<sup>th</sup> variable by assigning those expressions to the variables (§<a href="#Assignments">Assignments</a>).
is initialized to the value of the n<sup>th</sup> expression. Otherwise, each variable is initialized to its <i>zero value</i>
Otherwise, each variable is initialized to the <i>zero</i> <a href="#The_zero_value">The zero value</a>).
of the type (§<a href="#The_zero_value">The zero value</a>).
The expressions can be general expressions; they need not be constants.
</p> </p>
<p> <p>
Either the type or the expression list must be present. If the If the type is present, each variable is given that type.
type is present, it sets the type of each variable and the expressions Otherwise, the types are deduced from the assignment
(if any) must be assignment-compatible to that type. If the type of the expression list.
is absent, the variables take the types of the corresponding
expressions.
</p> </p>
<p> <p>
If the type is absent and the corresponding expression is a constant If the type is absent and the corresponding expression is a constant
expression of ideal integer or ideal float type, the type of the expression of ideal integer, float, or string type, the type of the
declared variable is <code>int</code> or <code>float</code> declared variable is <code>int</code>, <code>float</code>,
respectively: or <code>string</code> respectively:
</p> </p>
<pre> <pre>
var i = 0 // i has type int var i = 0 // i has type int
var f = 3.1415 // f has type float var f = 3.1415 // f has type float
var s = "OMDB" // s has type string
</pre> </pre>
<h3 id="Short_variable_declarations">Short variable declarations</h3> <h3 id="Short_variable_declarations">Short variable declarations</h3>
A <i>short variable declaration</i> uses the syntax A <i>short variable declaration</i> uses the syntax:
<pre class="ebnf"> <pre class="ebnf">
ShortVarDecl = IdentifierList ":=" ExpressionList . ShortVarDecl = IdentifierList ":=" ExpressionList .
</pre> </pre>
and is shorthand for the declaration syntax It is a shorthand for a regular variable declaration with
initializer expressions but no types:
<pre class="grammar"> <pre class="grammar">
"var" IdentifierList = ExpressionList . "var" IdentifierList = ExpressionList .
...@@ -1591,24 +1591,11 @@ and is shorthand for the declaration syntax ...@@ -1591,24 +1591,11 @@ and is shorthand for the declaration syntax
i, j := 0, 10; i, j := 0, 10;
f := func() int { return 7; } f := func() int { return 7; }
ch := make(chan int); ch := make(chan int);
</pre>
<p>
Unlike regular variable declarations, short variable declarations
can be used, by analogy with tuple assignment (§<a href="#Assignments">Assignments</a>), to
receive the individual elements of a multi-valued expression such
as a call to a multi-valued function. In this form, the ExpressionList
must be a single such multi-valued expression, the number of
identifiers must equal the number of values, and the declared
variables will be assigned the corresponding values.
</p>
<pre>
r, w := os.Pipe(fd); // os.Pipe() returns two values r, w := os.Pipe(fd); // os.Pipe() returns two values
</pre> </pre>
<p> <p>
A short variable declaration may redeclare variables provided they Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
were originally declared in the same block with the same type, and at were originally declared in the same block with the same type, and at
least one of the variables is new. As a consequence, redeclaration least one of the variables is new. As a consequence, redeclaration
can only appear in a multi-variable short declaration. can only appear in a multi-variable short declaration.
...@@ -3133,7 +3120,9 @@ assigns the first value to <code>x</code> and the second to <code>y</code>. ...@@ -3133,7 +3120,9 @@ assigns the first value to <code>x</code> and the second to <code>y</code>.
<p> <p>
In the second form, the number of operands on the left must equal the number In the second form, the number of operands on the left must equal the number
of expressions on the right, each of which must be single-valued. of expressions on the right, each of which must be single-valued, and the
<i>n</i>th expression on the right is assigned to the <i>n</i>th
operand on the left.
The expressions on the right are evaluated before assigning to The expressions on the right are evaluated before assigning to
any of the operands on the left, but otherwise the evaluation any of the operands on the left, but otherwise the evaluation
order is unspecified. order is unspecified.
...@@ -4141,7 +4130,7 @@ func main() { ...@@ -4141,7 +4130,7 @@ func main() {
When memory is allocated to store a value, either through a declaration When memory is allocated to store a value, either through a declaration
or <code>new()</code>, and no explicit initialization is provided, the memory is or <code>new()</code>, and no explicit initialization is provided, the memory is
given a default initialization. Each element of such a value is given a default initialization. Each element of such a value is
set to the zero value for its type: <code>false</code> for booleans, set to the <i>zero value</i> for its type: <code>false</code> for booleans,
<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code> <code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
for strings, and <code>nil</code> for pointers and interfaces. for strings, and <code>nil</code> for pointers and interfaces.
This initialization is done recursively, so for instance each element of an This initialization is done recursively, so for instance each element of an
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment