Skip to content

Commit bc405df

Browse files
committed
spec: allow embedding overlapping interfaces
Updates #6977. Change-Id: I6eda4be550e7c7ea1e1bac3222850002d90a81a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/190378 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 29d3c56 commit bc405df

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

doc/go_spec.html

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of July 31, 2019",
3+
"Subtitle": "Version of Aug 26, 2019",
44
"Path": "/ref/spec"
55
}-->
66

@@ -1244,16 +1244,15 @@ <h3 id="Interface_types">Interface types</h3>
12441244
</p>
12451245

12461246
<pre class="ebnf">
1247-
InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
1248-
MethodSpec = MethodName Signature | InterfaceTypeName .
1247+
InterfaceType = "interface" "{" { ( MethodSpec | InterfaceTypeName ) ";" } "}" .
1248+
MethodSpec = MethodName Signature .
12491249
MethodName = identifier .
12501250
InterfaceTypeName = TypeName .
12511251
</pre>
12521252

12531253
<p>
1254-
As with all method sets, in an interface type, each method must have a
1255-
<a href="#Uniqueness_of_identifiers">unique</a>
1256-
non-<a href="#Blank_identifier">blank</a> name.
1254+
An interface type may specify methods <i>explicitly</i> through method specifications,
1255+
or it may <i>embed</i> methods of other interfaces through interface type names.
12571256
</p>
12581257

12591258
<pre>
@@ -1265,6 +1264,11 @@ <h3 id="Interface_types">Interface types</h3>
12651264
}
12661265
</pre>
12671266

1267+
<p>
1268+
The name of each explicitly specified method must be <a href="#Uniqueness_of_identifiers">unique</a>
1269+
and not <a href="#Blank_identifier">blank</a>.
1270+
</p>
1271+
12681272
<pre>
12691273
interface {
12701274
String() string
@@ -1280,9 +1284,9 @@ <h3 id="Interface_types">Interface types</h3>
12801284
</p>
12811285

12821286
<pre>
1283-
func (p T) Read(p []byte) (n int, err error) { return … }
1284-
func (p T) Write(p []byte) (n int, err error) { return … }
1285-
func (p T) Close() error { return … }
1287+
func (p T) Read(p []byte) (n int, err error)
1288+
func (p T) Write(p []byte) (n int, err error)
1289+
func (p T) Close() error
12861290
</pre>
12871291

12881292
<p>
@@ -1332,27 +1336,41 @@ <h3 id="Interface_types">Interface types</h3>
13321336
<p>
13331337
An interface <code>T</code> may use a (possibly qualified) interface type
13341338
name <code>E</code> in place of a method specification. This is called
1335-
<i>embedding</i> interface <code>E</code> in <code>T</code>; it adds
1336-
all (exported and non-exported) methods of <code>E</code> to the interface
1337-
<code>T</code>.
1339+
<i>embedding</i> interface <code>E</code> in <code>T</code>.
1340+
The <a href="#Method_sets">method set</a> of <code>T</code> is the <i>union</i>
1341+
of the method sets of <code>T</code>’s explicitly declared methods and of
1342+
<code>T</code>’s embedded interfaces.
13381343
</p>
13391344

13401345
<pre>
1341-
type ReadWriter interface {
1342-
Read(b Buffer) bool
1343-
Write(b Buffer) bool
1346+
type Reader interface {
1347+
Read(p []byte) (n int, err error)
1348+
Close() error
13441349
}
13451350

1346-
type File interface {
1347-
ReadWriter // same as adding the methods of ReadWriter
1348-
Locker // same as adding the methods of Locker
1349-
Close()
1351+
type Writer interface {
1352+
Write(p []byte) (n int, err error)
1353+
Close() error
13501354
}
13511355

1352-
type LockedFile interface {
1353-
Locker
1354-
File // illegal: Lock, Unlock not unique
1355-
Lock() // illegal: Lock not unique
1356+
// ReadWriter's methods are Read, Write, and Close.
1357+
type ReadWriter interface {
1358+
Reader // includes methods of Reader in ReadWriter's method set
1359+
Writer // includes methods of Writer in ReadWriter's method set
1360+
}
1361+
</pre>
1362+
1363+
<p>
1364+
A <i>union</i> of method sets contains the (exported and non-exported)
1365+
methods of each method set exactly once, and methods with the
1366+
<a href="#Uniqueness_of_identifiers">same</a> names must
1367+
have <a href="#Type_identity">identical</a> signatures.
1368+
</p>
1369+
1370+
<pre>
1371+
type ReadCloser interface {
1372+
Reader // includes methods of Reader in ReadCloser's method set
1373+
Close() // illegal: signatures of Reader.Close and Close are different
13561374
}
13571375
</pre>
13581376

0 commit comments

Comments
 (0)