mirror of https://github.com/golang/go.git
spec: introduce notion of basic interface, misc. fine-tuning
A basic interface is a classical Go interface containing only methods or embedding basic interfaces. Use this to simplify rule about what interfaces may be used where. The term "basic interface" will also be useful when talking about various interfaces in general. Fix rule restricting union terms: as it was written it also excluded interface terms with non-empty method sets due to embedded non-interface types with methods. Split the large section on interfaces into three smaller pieces by introducing section titles. Change-Id: I142a4d5609eb48aaa0f7800b5b85c1d6c0703fcb Reviewed-on: https://go-review.googlesource.com/c/go/+/384994 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
ca3fae1e0e
commit
30501bbef9
|
|
@ -1226,11 +1226,15 @@ where a type element is a union of one or more <i>type terms</i>.
|
||||||
A type term is either a single type or a single underlying type.
|
A type term is either a single type or a single underlying type.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<h4 id="Basic_interfaces">Basic interfaces</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
In its most basic form an interface specifies a (possibly empty) list of methods.
|
In its most basic form an interface specifies a (possibly empty) list of methods.
|
||||||
The type set defined by such an interface is the set of types which implement all of
|
The type set defined by such an interface is the set of types which implement all of
|
||||||
those methods, and the corresponding <a href="#Method_sets">method set</a> consists
|
those methods, and the corresponding <a href="#Method_sets">method set</a> consists
|
||||||
exactly of the methods specified by the interface.
|
exactly of the methods specified by the interface.
|
||||||
|
Interfaces whose type sets can be defined entirely by a list of methods are called
|
||||||
|
<i>basic interfaces.</i>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
|
|
@ -1315,6 +1319,8 @@ they implement the <code>Locker</code> interface as well
|
||||||
as the <code>File</code> interface.
|
as the <code>File</code> interface.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<h4 id="Embedded_interfaces">Embedded interfaces</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
In a slightly more general form
|
In a slightly more general form
|
||||||
an interface <code>T</code> may use a (possibly qualified) interface type
|
an interface <code>T</code> may use a (possibly qualified) interface type
|
||||||
|
|
@ -1359,8 +1365,10 @@ type ReadCloser interface {
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
<h4 id="Generenal_interfaces">General interfaces</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Finally, in their most general form, an interface element may also be an arbitrary type term
|
In their most general form, an interface element may also be an arbitrary type term
|
||||||
<code>T</code>, or a term of the form <code>~T</code> specifying the underlying type <code>T</code>,
|
<code>T</code>, or a term of the form <code>~T</code> specifying the underlying type <code>T</code>,
|
||||||
or a union of terms <code>t<sub>1</sub>|t<sub>2</sub>|…|t<sub>n</sub></code>.
|
or a union of terms <code>t<sub>1</sub>|t<sub>2</sub>|…|t<sub>n</sub></code>.
|
||||||
Together with method specifications, these elements enable the precise
|
Together with method specifications, these elements enable the precise
|
||||||
|
|
@ -1462,21 +1470,21 @@ interface {
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Implementation restriction:
|
Implementation restriction:
|
||||||
A union with more than one term cannot contain interface types
|
A union with more than one term cannot contain the
|
||||||
with non-empty <a href="#Method_sets">method sets</a> or which
|
<a href="#Predeclared_identifiers">predeclared identifier</a> <code>comparable</code>
|
||||||
are or embed the <a href="#Predeclared_identifiers">predeclared identifier</a>
|
or interfaces that specify methods, or embed <code>comparable</code> or interfaces
|
||||||
<code>comparable</code>.
|
that specify methods.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Interfaces that contain non-interface types, terms of the form <code>~T</code>,
|
Interfaces that are not <a href="#Basic_interfaces">basic</a> may only be used as type
|
||||||
or unions may only be used as type constraints, or as elements of other interfaces used
|
constraints, or as elements of other interfaces used as constraints.
|
||||||
as constraints. They cannot be the types of values or variables, or components of other,
|
They cannot be the types of values or variables, or components of other,
|
||||||
non-interface types.
|
non-interface types.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
var x Floats // illegal: Floats is restricted by float32 and float64
|
var x Floats // illegal: Floats is not a basic interface
|
||||||
|
|
||||||
var x interface{} = Floats(nil) // illegal
|
var x interface{} = Floats(nil) // illegal
|
||||||
|
|
||||||
|
|
@ -1714,7 +1722,7 @@ The underlying type of <code>P</code> is <code>interface{}</code>.
|
||||||
<h3 id="Core_types">Core types</h3>
|
<h3 id="Core_types">Core types</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Each non-interface type <code>T</code> has a <i>core</i> type, which is the
|
Each non-interface type <code>T</code> has a <i>core type</i>, which is the same as the
|
||||||
<a href="#Underlying_types">underlying type</a> of <code>T</code>.
|
<a href="#Underlying_types">underlying type</a> of <code>T</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
@ -2665,9 +2673,9 @@ TypeConstraint = TypeElem .
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
If the constraint is an interface literal containing exactly one embedded type element
|
If the constraint is an interface literal of the form <code>interface{E}</code> where
|
||||||
<code>interface{E}</code>, in a type parameter list the enclosing <code>interface{ … }</code>
|
<code>E</code> is an embedded type element (not a method), in a type parameter list
|
||||||
may be omitted for convenience:
|
the enclosing <code>interface{ … }</code> may be omitted for convenience:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue