]> Cypherpunks repositories - gostls13.git/commitdiff
spec: extend type omission rules for composite literal element values
authorRobert Griesemer <gri@golang.org>
Fri, 9 Jan 2015 00:01:31 +0000 (16:01 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 20 Mar 2015 23:06:28 +0000 (23:06 +0000)
      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 <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
doc/go_spec.html

index 5a1b291433b1a5eaee48c74d1caef42aaaf599e8..d02697bd0acd814fcdd71daa0bdcf8509c6ac8ae 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of December 26, 2014",
+       "Subtitle": "Version of March 20, 2015",
        "Path": "/ref/spec"
 }-->
 
@@ -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 .
 </pre>
 
@@ -2357,17 +2356,21 @@ tmp[0 : n]
 
 <p>
 Within a composite literal of array, slice, or map type <code>T</code>,
-elements that are themselves composite literals may elide the respective
-literal type if it is identical to the element type of <code>T</code>.
-Similarly, elements that are addresses of composite literals may elide
-the <code>&amp;T</code> when the element type is <code>*T</code>.
+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 <code>T</code>.
+Similarly, elements or keys that are addresses of composite literals may elide
+the <code>&amp;T</code> when the element or key type is <code>*T</code>.
 </p>
 
 <pre>
-[...]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{&amp;Point{1.5, -3.5}, &amp;Point{0, 0}}
+[...]*Point{{1.5, -3.5}, {0, 0}}    // same as [...]*Point{&amp;Point{1.5, -3.5}, &amp;Point{0, 0}}
+
+map[Point]string{{0, 0}: "orig"}    // same as map[Point]string{Point{0, 0}: "orig"}
 </pre>
 
 <p>