From 2769356ddac0e102dfcad47e9856c3d52314b0a6 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 13 Jun 2011 16:47:33 -0700 Subject: [PATCH] go spec: specify constant conversions This is not a language change. Added paragraphs specifying which conversions yield results that are constants. R=r, rsc, iant, ken CC=golang-dev https://golang.org/cl/4515176 --- doc/go_spec.html | 70 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 30fce856acf..f82336a85bd 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -528,7 +528,8 @@ A constant value is represented by an character, or string literal, an identifier denoting a constant, -a constant expression, or +a constant expression, +a conversion with a result that is a constant, or the result value of some built-in functions such as unsafe.Sizeof applied to any value, cap or len applied to @@ -3227,8 +3228,42 @@ If the type starts with an operator it must be parenthesized:

-A value x can be converted to type T in any -of these cases: +A constant value x can be converted to +type T in any of these cases: +

+ + + +

+Converting a constant yields a typed constant as result. +

+ +
+uint(iota)               // iota value of type uint
+float32(2.718281828)     // 2.718281828 of type float32
+complex128(1)            // 1.0 + 0.0i of type complex128
+string('x')              // "x" of type string
+string(0x266c)           // "♬" of type string
+MyString("foo" + "bar")  // "foobar" of type MyString
+string([]byte{'a'})      // not a constant: []byte{'a'} is not a constant
+(*int)(nil)              // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
+int(1.2)                 // illegal: 1.2 cannot be represented as an int
+string(65.0)             // illegal: 65.0 is not an integer constant
+
+ +

+A non-constant value x can be converted to type T +in any of these cases:

-Specific rules apply to conversions between numeric types or to and from -a string type. +Specific rules apply to (non-constant) conversions between numeric types or +to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.

+

+There is no linguistic mechanism to convert between pointers and integers. +The package unsafe +implements this functionality under +restricted circumstances. +

+

Conversions between numeric types

+ +

+For the conversion of non-constant numeric values, the following rules apply: +

+
  1. When converting between integer types, if the value is a signed integer, it is @@ -3296,13 +3343,12 @@ of precision, but float32(x + 0.1) does not.

-In all conversions involving floating-point or complex values, +In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion -succeeds but the result value is -implementation-dependent. +succeeds but the result value is implementation-dependent.

-

Conversions to and from a string type

+

Conversions to and from a string type

  1. @@ -3360,12 +3406,6 @@ If the string is empty, the result is []int(nil).
-

-There is no linguistic mechanism to convert between pointers and integers. -The package unsafe -implements this functionality under -restricted circumstances. -

Constant expressions