From 6263484de505c73b60dd4dab234b7fff7db93eb2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Sat, 22 Sep 2012 05:53:53 +1000 Subject: [PATCH] [release-branch.go1] go spec: clean up section on selectors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ««« backport eb24cee7f21b go spec: clean up section on selectors - point out difference between selectors and qualified identifiers - differentiate between illegal selectors and run-time panics - use "indirect" as opposed to "dereference" consistently - add extra links Fixes #3779. R=r, rsc, iant, ken CC=golang-dev https://golang.org/cl/6326059 »»» --- doc/go_spec.html | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 2c6fcd1526..57f2db7402 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -15,7 +15,6 @@ TODO [ ] need explicit language about the result type of operations [ ] should probably write something about evaluation order of statements even though obvious -[ ] review language on implicit dereferencing --> @@ -2324,7 +2323,6 @@ Point{1, 2} m["foo"] s[i : j + 1] obj.color -math.Sin f.p[i].x() @@ -2332,7 +2330,9 @@ f.p[i].x()

Selectors

-A primary expression of the form +For a primary expression x +that is not a package name, the +selector expression

@@ -2340,17 +2340,20 @@ x.f
 

-denotes the field or method f of the value denoted by x -(or sometimes *x; see below). The identifier f -is called the (field or method) -selector; it must not be the blank identifier. -The type of the expression is the type of f. +denotes the field or method f of the value x +(or sometimes *x; see below). +The identifier f is called the (field or method) selector; +it must not be the blank identifier. +The type of the selector expression is the type of f. +If x is a package name, see the section on +qualified identifiers.

+

A selector f may denote a field or method f of a type T, or it may refer -to a field or method f of a nested anonymous field of -T. +to a field or method f of a nested +anonymous field of T. The number of anonymous fields traversed to reach f is called its depth in T. The depth of a field or method f @@ -2359,9 +2362,11 @@ The depth of a field or method f declared in an anonymous field A in T is the depth of f in A plus one.

+

The following rules apply to selectors:

+
  1. For a value x of type T or *T @@ -2373,18 +2378,26 @@ If there is not exactly one f< with shallowest depth, the selector expression is illegal.
  2. -For a variable x of type I -where I is an interface type, -x.f denotes the actual method with name f of the value assigned -to x if there is such a method. -If no value or nil was assigned to x, x.f is illegal. +For a variable x of type I where I +is an interface type, x.f denotes the actual method with name +f of the value assigned to x. +If there is no method with name f in the +method set of I, the selector +expression is illegal.
  3. In all other cases, x.f is illegal.
  4. +
  5. +If x is of pointer or interface type and has the value +nil, assigning to, evaluating, or calling x.f +causes a run-time panic. +
+

-Selectors automatically dereference pointers to structs. +Selectors automatically dereference +pointers to structs. If x is a pointer to a struct, x.y is shorthand for (*x).y; if the field y is also a pointer to a struct, x.y.z is shorthand @@ -2393,6 +2406,7 @@ If x contains an anonymous field of type *A, where A is also a struct type, x.f is a shortcut for (*x.A).f.

+

For example, given the declarations: