spec: use "core type" rather than "structural type"

This change in terminology prevents potential confusion
that migth be caused by associating "structural type"
with "structural typing"; the two are not connected.

Also, adjusted introductory paragraph of section on
constraint type inference: type inference goes in both
directions, from type parameter to core type and vice
versa. The previous description was not quite accurate.

Change-Id: If4ca300f525eea660f68486302619aa6ad5dbc2c
Reviewed-on: https://go-review.googlesource.com/c/go/+/384238
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Robert Griesemer 2022-02-08 10:41:27 -08:00
parent 9ed0d81fb5
commit 5d3476c3db
1 changed files with 25 additions and 20 deletions

View File

@ -2034,7 +2034,7 @@ interface{ int; string } // no specific types (intersection is empty)
</pre> </pre>
<p> <p>
An interface <code>T</code> is called <i>structural</i> if one of the following An interface <code>T</code> has a <i>core type</i> if one of the following
conditions is satisfied: conditions is satisfied:
</p> </p>
@ -2051,8 +2051,11 @@ direction.
</ol> </ol>
<p> <p>
A structural interface has a <i>structural type</i> which is, depending on the All other interfaces don't have a core type.
condition that is satisfied, either: </p>
<p>
The core type is, depending on the condition that is satisfied, either:
</p> </p>
<ol> <ol>
@ -2067,7 +2070,7 @@ depending on the direction of the directional channels present.
</ol> </ol>
<p> <p>
Examples of structural interfaces with their structural types: Examples of interfaces with core types:
</p> </p>
<pre> <pre>
@ -2079,7 +2082,7 @@ interface{ ~[]*data; String() string } // []*data
</pre> </pre>
<p> <p>
Examples of non-structural interfaces: Examples of interfaces whithout core types:
</p> </p>
<pre> <pre>
@ -4497,19 +4500,21 @@ min(1.0, 2) // illegal: default type float64 (for 1.0) doesn't match default
<h4 id="Constraint_type_inference">Constraint type inference</h3> <h4 id="Constraint_type_inference">Constraint type inference</h3>
<!-- <!--
The next paragraph needs to be updated for the new definition of structural type: The next paragraph needs to be updated for the new definition of core type:
The structural type of an interface is the single underlying type of its type set, The core type of an interface is the single underlying type of its type set,
if it exists. But for constraint type inference, if the type set consists of exactly if it exists. But for constraint type inference, if the type set consists of exactly
one type, we want to use that one type (which may be a defined type, different from one type, we want to use that one type (which may be a defined type, different from
its underlying == structural type). its underlying == core type).
--> -->
<p> <p>
Constraint type inference infers type arguments from already known Constraint type inference infers type arguments by considering type constraints.
type arguments by considering <a href="#Structure_of_interfaces">structural type constraints</a>: If a type parameter <code>P</code> has a constraint with a
if the structural type <code>T</code> of a structural constraint is parameterized, <a href="#Structure_of_interfaces">core type</a> <code>C</code>,
<a href="#Type_unification">unifying</a> a known type argument with <code>T</code> may <a href="#Type_unification">unifying</a> <code>P</code> with <code>C</code>
infer type arguments for other type parameters used by the structural type. may infer additional type arguments, either the type argument for <code>P</code>,
or if that is already known, possibly the type arguments for type parameters
used in <code>C</code>.
</p> </p>
<p> <p>
@ -4523,8 +4528,8 @@ For instance, consider the type parameter list with type parameters <code>List</
<p> <p>
Constraint type inference can deduce the type of <code>Elem</code> from the type argument Constraint type inference can deduce the type of <code>Elem</code> from the type argument
for <code>List</code> because <code>Elem</code> is a type parameter in the structural constraint for <code>List</code> because <code>Elem</code> is a type parameter in the core type
<code>~[]Elem</code> for <code>List</code>. <code>[]Elem</code> of <code>List</code>.
If the type argument is <code>Bytes</code>: If the type argument is <code>Bytes</code>:
</p> </p>
@ -4533,7 +4538,7 @@ type Bytes []byte
</pre> </pre>
<p> <p>
unifying the underlying type of <code>Bytes</code> with the structural constraint means unifying the underlying type of <code>Bytes</code> with the core type means
unifying <code>[]byte</code> with <code>[]Elem</code>. That unification succeeds and yields unifying <code>[]byte</code> with <code>[]Elem</code>. That unification succeeds and yields
the <a href="#Type_unification">substitution map</a> entry the <a href="#Type_unification">substitution map</a> entry
<code>Elem</code> &RightArrow; <code>byte</code>. <code>Elem</code> &RightArrow; <code>byte</code>.
@ -4548,8 +4553,8 @@ substitution map <i>M</i>
<ol> <ol>
<li> <li>
For all type parameters with a structural constraint, unify the type parameter with the structural For all type parameters with a core type, unify the type parameter with the core
type of its constraint. If any unification fails, constraint type inference fails. type. If any unification fails, constraint type inference fails.
</li> </li>
<li> <li>
@ -4584,7 +4589,7 @@ the initial substitution map <i>M</i> contains the entry <code>A</code> &RightAr
<p> <p>
In the first phase, the type parameters <code>B</code> and <code>C</code> are unified In the first phase, the type parameters <code>B</code> and <code>C</code> are unified
with the structural type of their respective constraints. This adds the entries with the core type of their respective constraints. This adds the entries
<code>B</code> &RightArrow; <code>[]C</code> and <code>C</code> &RightArrow; <code>*A</code> <code>B</code> &RightArrow; <code>[]C</code> and <code>C</code> &RightArrow; <code>*A</code>
to <i>M</i>. to <i>M</i>.
@ -5192,7 +5197,7 @@ as for non-constant <code>x</code>.
<p> <p>
Converting a constant to a type that is not a <a href="#Type_parameters">type parameter</a> Converting a constant to a type that is not a <a href="#Type_parameters">type parameter</a>
yields a typed constant. yields a typed constant.
Converting a constant to a type parameter yields a non-constant value of that type. Converting a constant to a type parameter yields a non-constant value of that type.
</p> </p>
<pre> <pre>