From: Robert Griesemer Date: Fri, 9 Jan 2015 00:01:31 +0000 (-0800) Subject: spec: extend type omission rules for composite literal element values X-Git-Tag: go1.5beta1~1481 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7727dee44565bed441c9fc09a2e2441ecd6b9fe7;p=gostls13.git spec: extend type omission rules for composite literal element values to map element keys Composite literals containing element values that are themselves composite literals may leave away the element's literal types if they are identical to the enclosing composite literal's element type. (http://golang.org/ref/spec#Composite_literals) When we made this change, we forgot to apply the analogous rule to map literal keys. This change generalizes that rule. Added more examples, including one showing the recursive application of the elision rules. This is a fully backward-compatible language change. It was discussed some time back. Fixes #8589. To be submitted once all compilers accept the extension. Change-Id: I4d45b64b5970f0d5501572945d5a097e64a9458b Reviewed-on: https://go-review.googlesource.com/2591 Reviewed-by: Russ Cox Reviewed-by: Rob Pike --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 5a1b291433..d02697bd0a 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -2236,9 +2236,8 @@ LiteralType = StructType | ArrayType | "[" "..." "]" ElementType | LiteralValue = "{" [ ElementList [ "," ] ] "}" . ElementList = Element { "," Element } . Element = [ Key ":" ] Value . -Key = FieldName | ElementIndex . +Key = FieldName | Expression | LiteralValue . FieldName = identifier . -ElementIndex = Expression . Value = Expression | LiteralValue . @@ -2357,17 +2356,21 @@ tmp[0 : n]

Within a composite literal of array, slice, or map type T, -elements that are themselves composite literals may elide the respective -literal type if it is identical to the element type of T. -Similarly, elements that are addresses of composite literals may elide -the &T when the element type is *T. +elements or map keys that are themselves composite literals may elide the respective +literal type if it is identical to the element or key type of T. +Similarly, elements or keys that are addresses of composite literals may elide +the &T when the element or key type is *T.

-[...]Point{{1.5, -3.5}, {0, 0}}   // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
-[][]int{{1, 2, 3}, {4, 5}}        // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
+[...]Point{{1.5, -3.5}, {0, 0}}     // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
+[][]int{{1, 2, 3}, {4, 5}}          // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
+[][]Point{{{0, 1}, {1, 2}}}         // same as [][]Point{[]Point{Point{0, 1}, Point{1, 2}}}
+map[string]Point{"orig": {0, 0}}    // same as map[string]Point{"orig": Point{0, 0}}
 
-[...]*Point{{1.5, -3.5}, {0, 0}}  // same as [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}
+[...]*Point{{1.5, -3.5}, {0, 0}}    // same as [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}
+
+map[Point]string{{0, 0}: "orig"}    // same as map[Point]string{Point{0, 0}: "orig"}