]> Cypherpunks repositories - gostls13.git/commitdiff
spec: add example for method value in case of embedded method
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 22 Aug 2021 05:07:14 +0000 (12:07 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Mon, 23 Aug 2021 17:29:25 +0000 (17:29 +0000)
So it's clear to the reader that if "M" is a promoted method from
embedded field "T", then "x.M" will be expanded to "x.T.M" during the
evaluation of the method value.

Fixes #47863

Change-Id: Id3b82127a2054584b6842c487f6e15c3102dc9fe
Reviewed-on: https://go-review.googlesource.com/c/go/+/344209
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
doc/go_spec.html

index fd5fee46eb220c1111d8aa33853037f751d4bdbb..22b616134ac48d24c2acefc637b0c8944c72401d 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of Jul 26, 2021",
+       "Subtitle": "Version of Aug 23, 2021",
        "Path": "/ref/spec"
 }-->
 
@@ -3000,6 +3000,18 @@ method value; the saved copy is then used as the receiver in any calls,
 which may be executed later.
 </p>
 
+<pre>
+type S struct { *T }
+type T int
+func (t T) M() { print(t) }
+
+t := new(T)
+s := S{T: t}
+f := t.M                    // receiver *t is evaluated and stored in f
+g := s.M                    // receiver *(s.T) is evaluated and stored in g
+*t = 42                     // does not affect stored receivers in f and g
+</pre>
+
 <p>
 The type <code>T</code> may be an interface or non-interface type.
 </p>