From e30ebaab0bd5d95178f77cf40998ab14a0341d17 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 18 Nov 2021 17:52:24 -0800 Subject: [PATCH] spec: add section on the structure of interfaces This change introduces the notion of a structural interface and its corresponding structural type. Change-Id: Ib5442dfd04cb5950b4467428cae51849f8922272 Reviewed-on: https://go-review.googlesource.com/c/go/+/365474 Trust: Robert Griesemer Run-TryBot: Robert Griesemer Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 66 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 2120985b3b..bf589f0ae6 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1269,7 +1269,6 @@ func(int, int, float64) (float64, *[]int) func(n int) func(p *T) -

Interface types

@@ -1655,8 +1654,8 @@ ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType .

The optional <- operator specifies the channel direction, -send or receive. If no direction is given, the channel is -bidirectional. +send or receive. If a direction is given, the channel is directional, +otherwise it is bidirectional. A channel may be constrained only to send or only to receive by assignment or explicit conversion. @@ -1836,7 +1835,6 @@ created by distinct type definitions; are different because B0 is different from []string.

-

Assignability

@@ -1928,6 +1926,66 @@ x T x is not representable by a value of T because 1e1000 float64 1e1000 overflows to IEEE +Inf after rounding +

Structural interfaces

+ +

+An interface T is called structural if one of the following +conditions is satisfied: +

+ +
    +
  1. +There is a single type U which is the underlying type +of all types in the type set of T; or +
  2. +
  3. +the type set of T contains only channel types +with identical element type E, and all directional channels have the same +direction. +
  4. +
+ +

+A structural interface has a structural type which is, depending on the +condition that is satisfied, either: +

+ +
    +
  1. +the type U; or +
  2. +
  3. +the type chan E if T contains only bidirectional +channels, or the type chan<- E or <-chan E +depending on the direction of the directional channels present. +
  4. +
+ +

+Examples of structural interfaces with their structural types: +

+ +
+type Celsius float32
+type Kelvin  float32
+
+interface{ int }                          // int
+interface{ Celsius|Kelvin }               // float32
+interface{ ~chan int }                    // chan int
+interface{ ~chan int|~chan<- int }        // chan<- int
+interface{ ~[]*data; String() string }    // []*data
+
+ +

+Examples of non-structural interfaces: +

+ +
+interface{}                               // no single underlying type
+interface{ Celsius|float64 }              // no single underlying type
+interface{ chan int | chan<- string }     // channels have different element types
+interface{ <-chan int | chan<- int }      // directional channels have different directions
+

Blocks

-- 2.50.0