]> Cypherpunks repositories - gostls13.git/commitdiff
add some words (written by rsc) about the state of typed constants.
authorRob Pike <r@golang.org>
Wed, 25 Mar 2009 02:16:42 +0000 (19:16 -0700)
committerRob Pike <r@golang.org>
Wed, 25 Mar 2009 02:16:42 +0000 (19:16 -0700)
DELTA=31  (31 added, 0 deleted, 0 changed)
OCL=26709
CL=26716

doc/go_spec.html

index 29372493c8f92a16915cd440564921935e597f35..1f08a551c521aa94d0a16906b754dfaa22050f95 100644 (file)
@@ -2950,6 +2950,37 @@ floating point variable, while <code>-1e12</code> can be assigned to a
 but not <code>uint64</code> or <code>string</code>.
 </p>
 
+<p>
+If a typed constant expression evaluates to a value that is not
+representable by that type, the compiler reports an error.
+</p>
+
+<pre>
+uint8(-1)         // error, out of range
+uint8(100) * 100  // error, out of range
+</pre>
+
+<p>
+The size of the mask used by the unary bitwise complement
+operator in a typed constant expression is equal to the size of the
+expression's type.  In an ideal constant expression, the bitwise
+complement operator inverts all the bits, producing a negative value.
+</p>
+
+<pre>
+^1          // ideal constant, equal to -2
+uint8(^1)   // error, same as uint8(-2), out of range
+^uint8(1)   // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
+int8(^1)    // same as int8(-2)
+^int8(1)    // error, same as 0xFF ^ int8(1) = int8(0xFE), out of range
+</pre>
+
+<p>
+TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
+Also it may be possible to make typed constants more like variables, at the cost of fewer
+overflow etc. errors being caught.
+</p>
+
 <hr/>
 
 <h2>Statements</h2>