Commit bb29c5a1 authored by Robert Griesemer's avatar Robert Griesemer

spec: clarify embedding of interfaces

Fixes #7886.

LGTM=iant, r, rsc
R=r, iant, rsc, ken
CC=golang-codereviews
https://golang.org/cl/149010043
parent 74c0de8f
<!--{ <!--{
"Title": "The Go Programming Language Specification", "Title": "The Go Programming Language Specification",
"Subtitle": "Version of September 19, 2014", "Subtitle": "Version of September 25, 2014",
"Path": "/ref/spec" "Path": "/ref/spec"
}--> }-->
...@@ -1154,11 +1154,11 @@ interface{} ...@@ -1154,11 +1154,11 @@ interface{}
<p> <p>
Similarly, consider this interface specification, Similarly, consider this interface specification,
which appears within a <a href="#Type_declarations">type declaration</a> which appears within a <a href="#Type_declarations">type declaration</a>
to define an interface called <code>Lock</code>: to define an interface called <code>Locker</code>:
</p> </p>
<pre> <pre>
type Lock interface { type Locker interface {
Lock() Lock()
Unlock() Unlock()
} }
...@@ -1174,28 +1174,35 @@ func (p T) Unlock() { … } ...@@ -1174,28 +1174,35 @@ func (p T) Unlock() { … }
</pre> </pre>
<p> <p>
they implement the <code>Lock</code> interface as well they implement the <code>Locker</code> interface as well
as the <code>File</code> interface. as the <code>File</code> interface.
</p> </p>
<p> <p>
An interface may use an interface type name <code>T</code> An interface <code>T</code> may use a (possibly qualified) interface type
in place of a method specification. name <code>E</code> in place of a method specification. This is called
The effect, called embedding an interface, <i>embedding</i> interface <code>E</code> in <code>T</code>; it adds
is equivalent to enumerating the methods of <code>T</code> explicitly all (exported and non-exported) methods of <code>E</code> to the interface
in the interface. <code>T</code>.
</p> </p>
<pre> <pre>
type ReadWrite interface { type ReadWriter interface {
Read(b Buffer) bool Read(b Buffer) bool
Write(b Buffer) bool Write(b Buffer) bool
} }
type File interface { type File interface {
ReadWrite // same as enumerating the methods in ReadWrite ReadWriter // same as adding the methods of ReadWriter
Lock // same as enumerating the methods in Lock Locker // same as adding the methods of Locker
Close() Close()
} }
type LockedFile interface {
Locker
File // illegal: Lock, Unlock not unique
Lock() // illegal: Lock not unique
}
</pre> </pre>
<p> <p>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment