1
0
mirror of https://github.com/golang/go synced 2024-11-25 05:27:57 -07:00

rewrite type rules for expressions and add shift examples

DELTA=48  (22 added, 0 deleted, 26 changed)
OCL=33657
CL=33668
This commit is contained in:
Rob Pike 2009-08-21 14:18:08 -07:00
parent cfa52e5e0b
commit 83cbca5657

View File

@ -351,7 +351,7 @@ integer literals.
<h3 id="String_literals">String literals</h3> <h3 id="String_literals">String literals</h3>
<p> <p>
String literals represent <i>ideal string</i> values. Ideal strings don't String literals represent <i>ideal string</i> values. Ideal strings do not
have a named type but they are compatible with type <code>string</code> have a named type but they are compatible with type <code>string</code>
<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>). <a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
There are two forms: raw string literals and interpreted string There are two forms: raw string literals and interpreted string
@ -1037,7 +1037,7 @@ the zero value for the channel's type. After at least one such zero value has b
received, <code>closed(c)</code> returns true. received, <code>closed(c)</code> returns true.
</p> </p>
<h2 id="General_properties_of_types_and_values">General properties of types and values</h2> <h2 id="Properties_of_types_and_values">Properties of types and values</h2>
<p> <p>
Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>. Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>.
@ -1215,6 +1215,8 @@ Function values are equal if they refer to the same function.
<li> <li>
Channel and map values are equal if they were created by the same call to <code>make</code> Channel and map values are equal if they were created by the same call to <code>make</code>
<a href="#Making_slices">Making slices</a>, maps, and channels). <a href="#Making_slices">Making slices</a>, maps, and channels).
When comparing two values of channel type, the channel value types
must be compatible but the channel direction is ignored.
</li> </li>
<li> <li>
Interface values may be compared if they have compatible static types. Interface values may be compared if they have compatible static types.
@ -2391,30 +2393,50 @@ unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
</pre> </pre>
<p> <p>
The operand types in binary operations must be compatible, with the following exceptions: Comparisons are discussed elsewhere
<a href="#Comparison_compatibility">Comparison compatibility</a>).
For other binary operators, the
operand types must be identical
<a href="#Properties_of_types_and_values">Properties of types and values</a>)
unless the operation involves
channels, shifts, or ideal constants.
</p> </p>
<ul>
<li>Except in shift expressions, if one operand has numeric type and the other operand is
an ideal number, the ideal number is converted to match the type of
the other operand (§<a href="#Expressions">Expressions</a>).</li>
<li>Except in shift expressions, if both operands are ideal numbers and one is an <p>
ideal float, the other is converted to ideal float In a channel send, the first operand is always a channel and the
(relevant for <code>/</code> and <code>%</code>).</li> second is a value of the channel's element type.
</p>
<li>In shift operations, the above rules apply to the left operand of the shift as <p>
if the shift operator and the right operand where not present. Except for shift operations,
if one operand has ideal type and the other operand does not,
the ideal operand is converted converted to match the type of
the other operand (§<a href="#Expressions">Expressions</a>).
If both operands are ideal numbers and one is an
ideal float, the other is converted to ideal float
(relevant for <code>/</code> and <code>%</code>).
</p>
<li>The right operand in a shift operation must be always be of unsigned integer type <p>
or an ideal number that can be safely converted into an unsigned integer type The right operand in a shift operation must have unsigned integer type
<a href="#Arithmetic_operators">Arithmetic operators</a>).</li> or be an ideal number that can be converted to unsigned integer type
<a href="#Arithmetic_operators">Arithmetic operators</a>).
</p>
<li>The operands in channel sends differ in type: one is always a channel and the <p>
other is a variable or value of the channel's element type.</li> If the left operand of a non-constant shift operation is an ideal number,
the type of the ideal number
is what it would be if the shift operation were replaced by the left operand alone.
</p>
<li>When comparing two operands of channel type, the channel value types <pre>
must be compatible but the channel direction is ignored.</li> var s uint = 33;
</ul> var i = 1&lt;&lt;s; // 1 has type int
var j = int32(1&lt;&lt;s); // 1 has type int32; j == 0
var u = uint64(1&lt;&lt;s); // 1 has type uint64; u == 1&lt;&lt;33
var f = float(1&lt;&lt;s); // illegal: 1 has type float, cannot shift
var g = float(1&lt;&lt;33); // legal; 1&lt;&lt;33 is a constant shift operation; g == 1&lt;&lt;33
</pre>
<p> <p>
Unary operators have the highest precedence. Unary operators have the highest precedence.