From 21d03496e7b6e7c32a0b6f5a76abab0c9e9c086b Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 24 Mar 2009 19:16:42 -0700 Subject: [PATCH] add some words (written by rsc) about the state of typed constants. DELTA=31 (31 added, 0 deleted, 0 changed) OCL=26709 CL=26716 --- doc/go_spec.html | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/go_spec.html b/doc/go_spec.html index 29372493c8f..1f08a551c52 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