From: Rob Pike Date: Wed, 25 Mar 2009 02:16:42 +0000 (-0700) Subject: add some words (written by rsc) about the state of typed constants. X-Git-Tag: weekly.2009-11-06~1967 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=21d03496e7b6e7c32a0b6f5a76abab0c9e9c086b;p=gostls13.git add some words (written by rsc) about the state of typed constants. DELTA=31 (31 added, 0 deleted, 0 changed) OCL=26709 CL=26716 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 29372493c8..1f08a551c5 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -2950,6 +2950,37 @@ floating point variable, while -1e12 can be assigned to a but not uint64 or string.

+

+If a typed constant expression evaluates to a value that is not +representable by that type, the compiler reports an error. +

+ +
+uint8(-1)         // error, out of range
+uint8(100) * 100  // error, out of range
+
+ +

+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. +

+ +
+^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
+
+ +

+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. +

+

Statements