From 30501bbef9fcfc9d53e611aaec4d20bb3cdb8ada Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 10 Feb 2022 16:02:48 -0800 Subject: [PATCH] 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 Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 25a2fd96a1..3405b7d887 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1226,11 +1226,15 @@ where a type element is a union of one or more type terms. A type term is either a single type or a single underlying type.

+

Basic interfaces

+

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 those methods, and the corresponding method set consists exactly of the methods specified by the interface. +Interfaces whose type sets can be defined entirely by a list of methods are called +basic interfaces.

@@ -1315,6 +1319,8 @@ they implement the Locker interface as well
 as the File interface.
 

+

Embedded interfaces

+

In a slightly more general form an interface T may use a (possibly qualified) interface type @@ -1359,8 +1365,10 @@ type ReadCloser interface { }

+

General interfaces

+

-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 T, or a term of the form ~T specifying the underlying type T, or a union of terms t1|t2|…|tn. Together with method specifications, these elements enable the precise @@ -1462,21 +1470,21 @@ interface {

Implementation restriction: -A union with more than one term cannot contain interface types -with non-empty method sets or which -are or embed the predeclared identifier -comparable. +A union with more than one term cannot contain the +predeclared identifier comparable +or interfaces that specify methods, or embed comparable or interfaces +that specify methods.

-Interfaces that contain non-interface types, terms of the form ~T, -or unions may only be used as type constraints, or as elements of other interfaces used -as constraints. They cannot be the types of values or variables, or components of other, +Interfaces that are not basic may only be used as type +constraints, or as elements of other interfaces used as constraints. +They cannot be the types of values or variables, or components of other, non-interface types.

-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
 
@@ -1714,7 +1722,7 @@ The underlying type of P is interface{}.
 

Core types

-Each non-interface type T has a core type, which is the +Each non-interface type T has a core type, which is the same as the underlying type of T.

@@ -2665,9 +2673,9 @@ TypeConstraint = TypeElem .

-If the constraint is an interface literal containing exactly one embedded type element -interface{E}, in a type parameter list the enclosing interface{ … } -may be omitted for convenience: +If the constraint is an interface literal of the form interface{E} where +E is an embedded type element (not a method), in a type parameter list +the enclosing interface{ … } may be omitted for convenience: