From 164a7bceeb67af9490d970e3c6e665198c6e1c9a Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 30 Sep 2009 12:00:25 -0700 Subject: [PATCH] - completed section on built-in functions - moved Conversions section out of built-in functions and into expressions - fixed syntax of conversions (parens are not mandatory if the type is not a TypeName) (this is the only change to the Conversions section; the rest of the text is just moved; old line: 4043, new line: 3078) - fixed syntax of composite literals (parens are allowed around LiteralType) DELTA=239 (115 added, 98 deleted, 26 changed) OCL=35118 CL=35159 --- doc/go_spec.html | 261 +++++++++++++++++++++++++---------------------- 1 file changed, 139 insertions(+), 122 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index b9982e0c9be..d49a4a07557 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -69,9 +69,6 @@ Todo (struct{T} vs struct {T T} vs struct {t T}) [ ] need explicit language about the result type of operations [ ] may want to have some examples for the types of shift operations -[ ] document illegality of package-external tuple assignments to structs - w/ private fields: P.T(1, 2) illegal since same as P.T(a: 1, b: 2) for - a T struct { a b int }. [ ] should probably write something about evaluation order of statements even though obvious [ ] specify iteration direction for range clause @@ -1097,7 +1094,7 @@ to receive. This constraint is called a channel's direction; either

-chan T            // can be used to send and receive values of type T
+chan T         // can be used to send and receive values of type T
 chan<- float   // can only be used to send floats
 <-chan int     // can only be used to receive ints
 
@@ -1121,12 +1118,8 @@ or absent, the communication succeeds only when both a sender and receiver are r

-For a channel c, the predefined function close(c) -marks the channel as unable to accept more -values through a send operation. After any previously -sent values have been received, receives will return -the zero value for the channel's type. After at least one such zero value has been -received, closed(c) returns true. +A channel may be closed and tested for closure with the built-in functions +close and closed.

Properties of types and values

@@ -1946,7 +1939,7 @@ a single expression or a key-value pair.
 CompositeLit  = LiteralType "{" [ ElementList ] "}" .
 LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
-                SliceType | MapType | TypeName .
+                SliceType | MapType | TypeName | "(" LiteralType ")" .
 ElementList   = Element { "," Element } [ "," ] .
 Element       = [ Key ":" ] Value .
 Key           = FieldName | Index .
@@ -2134,10 +2127,15 @@ as they are accessible.
 
 

Primary expressions

+

+Primary expressions are the operands for unary and binary expressions. +

+
 PrimaryExpr =
 	Operand |
 	Conversion |
+	BuiltinCall |
 	PrimaryExpr Selector |
 	PrimaryExpr Index |
 	PrimaryExpr Slice |
@@ -3059,6 +3057,84 @@ It is legal to derive a function value from a method of an interface type.
 The resulting function takes an explicit receiver of that interface type.
 

+

Conversions

+ +

+Conversions are expressions of the form T(x) +where T is a type and x is an expression +that can be converted to type T. +

+ +
+Conversion = LiteralType "(" Expression ")" .
+
+ +

+The following conversion rules apply: +

+
    +
  • +1) The conversion succeeds if the value is assignment compatible +with type T. +
  • +
  • +2) The conversion succeeds if the value would be assignment compatible +with type T if the value's type, or T, or any of their component +types were unnamed. +
  • +
  • +3) Between integer types: If the value is a signed quantity, it is +sign extended to implicit infinite precision; otherwise it is zero +extended. It is then truncated to fit in the result type's size. +For example, if x := uint16(0x10F0), then uint32(int8(x)) == 0xFFFFFFF0. +The conversion always yields a valid value; there is no indication of overflow. +
  • +
  • +4) Between integer and floating-point types, or between floating-point types: +When converting a floating-point number to an integer, the fraction is discarded +(truncation towards zero). +In all conversions involving floating-point values, if the result type cannot represent the +value the conversion succeeds but the result value is unspecified. +This behavior may change. +
  • +
  • +5) Strings permit three special conversions: +
  • +
  • +5a) Converting an integer value yields a string containing the UTF-8 +representation of the integer. + +
    +string(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
    +
    + +
  • +
  • +5b) Converting a slice of integers yields a string that is the +concatenation of the individual integers converted to strings. +If the slice value is nil, the result is the empty string. +
    +string([]int{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
    +
  • + +
  • +5c) Converting a slice of bytes yields a string whose successive +bytes are those of the slice. If the slice value is nil, +the result is the empty string. + +
    +string([]byte{'h', 'e', 'l', 'l', 'o'})  // "hello"
    +
    +
  • +
+ +

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

+

Constant expressions

@@ -3978,30 +4054,47 @@ for i := 0; i <= 3; i++ { }

-

Predeclared functions

- +

Built-in functions

+ +

+A small number of built-in functions are +predeclared. +They are called like any other function but some of them +accept a type instead of an expression as the first argument. +

+ +
+BuiltinCall = identifier "(" [ BuiltinArgs ] ")" .
+BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
+
+ +

Close and closed

+ +

+For a channel c, the predefined function close(c) +marks the channel as unable to accept more +values through a send operation. After any previously +sent values have been received, receive operations will return +the zero value for the channel's type. After at least one such zero value has been +received, closed(c) returns true. +

Length and capacity

+

+The built-in functions len and cap take arguments +of various types and return a result of type int. +The implementation guarantees that the result always fits into an int. +

+
 Call      Argument type        Result
 
-len(s)    string               string length (in bytes)
+len(s)    string type          string length in bytes
           [n]T, *[n]T          array length (== n)
           []T                  slice length
           map[K]T              map length (number of defined keys)
-          chan T               number of elements sent queued in channel buffer
+          chan T               number of elements queued in channel buffer
 
 cap(s)    [n]T, *[n]T          array length (== n)
           []T                  slice capacity
@@ -4009,107 +4102,16 @@ cap(s)    [n]T, *[n]T          array length (== n)
 

-The type of the result is always int and the -implementation guarantees that -the result always fits into an int. -

-The capacity of a slice or map is the number of elements for which there is -space allocated in the underlying array (for a slice) or map. For a slice -s, at any time the following relationship holds: +The capacity of a slice is the number of elements for which there is +space allocated in the underlying array. +At any time the following relationship holds: +

 0 <= len(s) <= cap(s)
 
-

Conversions

- -

-Conversions look like function calls of the form -

- -
-T(value)
-
- -

-where T is a type -and value is an expression -that can be converted to a value -of result type T. -

- -
-Conversion = ( TypeName | "(" Type ")" ) Expression .
-
- -

-The following conversion rules apply: -

- - -

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

- -

Allocation

@@ -4180,10 +4182,25 @@ c := make(chan int, 10); # channel with a buffer size of 10 m := make(map[string] int, 100); # map with initial space for 100 elements

+ +

Bootstrapping

+

-TODO: Need syntax that permits a type as first argument for built-ins. +Current implementations provide several built-in functions useful during +bootstrapping. These functions are documented for completeness but are not +guaranteed to stay in the language. They do not return a result.

+
+Call       Behavior
+
+print      prints all arguments; formatting of arguments is implementation-specific
+println    like print but prints spaces between arguments and a newline at the end
+panic      like print, aborts execution after printing
+panicln    like println, aborts execution after printing
+
+ +

Packages