Skip to content

Commit 6fb0731

Browse files
committed
spec: more adjustments/corrections
- Change section title from "Type parameters lists" to "Type parameter declarations" as the enclosing section is about declarations. - Correct section on parsing ambiguity in type parameter lists. - Rephrase paragraphs on type parameters for method receivers and adjust examples. - Remove duplicate prose in section on function argument type inference. - Clarified "after substitution" column in Instantiations section. Change-Id: Id76be9804ad96a3f1221e5c4942552dde015dfcb Reviewed-on: https://go-review.googlesource.com/c/go/+/390994 Trust: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 46f352d commit 6fb0731

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

doc/go_spec.html

+47-45
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ <h2 id="Types">Types</h2>
807807
<p>
808808
The language <a href="#Predeclared_identifiers">predeclares</a> certain type names.
809809
Others are introduced with <a href="#Type_declarations">type declarations</a>
810-
or <a href="#Type_parameter_lists">type parameter lists</a>.
810+
or <a href="#Type_parameter_declarations">type parameter lists</a>.
811811
<i>Composite types</i>&mdash;array, struct, pointer, function,
812812
interface, slice, map, and channel types&mdash;may be constructed using
813813
type literals.
@@ -1459,7 +1459,7 @@ <h4 id="General_interfaces">General interfaces</h4>
14591459
</pre>
14601460

14611461
<p>
1462-
In a union, a term cannot be a <a href="#Type_parameter_lists">type parameter</a>, and the type sets of all
1462+
In a union, a term cannot be a <a href="#Type_parameter_declarations">type parameter</a>, and the type sets of all
14631463
non-interface terms must be pairwise disjoint (the pairwise intersection of the type sets must be empty).
14641464
Given a type parameter <code>P</code>:
14651465
</p>
@@ -1769,7 +1769,7 @@ <h3 id="Core_types">Core types</h3>
17691769

17701770
<p>
17711771
By definition, a core type is never a <a href="#Type_definitions">defined type</a>,
1772-
<a href="#Type_parameter_lists">type parameter</a>, or
1772+
<a href="#Type_parameter_declarations">type parameter</a>, or
17731773
<a href="#Interface_types">interface type</a>.
17741774
</p>
17751775

@@ -1965,7 +1965,7 @@ <h3 id="Representability">Representability</h3>
19651965
<p>
19661966
A <a href="#Constants">constant</a> <code>x</code> is <i>representable</i>
19671967
by a value of type <code>T</code>,
1968-
where <code>T</code> is not a <a href="#Type_parameter_lists">type parameter</a>,
1968+
where <code>T</code> is not a <a href="#Type_parameter_declarations">type parameter</a>,
19691969
if one of the following conditions applies:
19701970
</p>
19711971

@@ -2105,6 +2105,7 @@ <h2 id="Declarations_and_scope">Declarations and scope</h2>
21052105
A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a
21062106
<a href="#Constant_declarations">constant</a>,
21072107
<a href="#Type_declarations">type</a>,
2108+
<a href="#Type_parameter_declarations">type parameter</a>,
21082109
<a href="#Variable_declarations">variable</a>,
21092110
<a href="#Function_declarations">function</a>,
21102111
<a href="#Labeled_statements">label</a>, or
@@ -2502,7 +2503,7 @@ <h4 id="Type_definitions">Type definitions</h4>
25022503
</pre>
25032504

25042505
<p>
2505-
If the type definition specifies <a href="#Type_parameter_lists">type parameters</a>,
2506+
If the type definition specifies <a href="#Type_parameter_declarations">type parameters</a>,
25062507
the type name denotes a <i>generic type</i>.
25072508
Generic types must be <a href="#Instantiations">instantiated</a> when they
25082509
are used.
@@ -2538,7 +2539,7 @@ <h4 id="Type_definitions">Type definitions</h4>
25382539
func (l *List[T]) Len() int { … }
25392540
</pre>
25402541

2541-
<h3 id="Type_parameter_lists">Type parameter lists</h3>
2542+
<h3 id="Type_parameter_declarations">Type parameter declarations</h3>
25422543

25432544
<p>
25442545
A type parameter list declares the <i>type parameters</i> of a generic function or type declaration.
@@ -2577,22 +2578,22 @@ <h3 id="Type_parameter_lists">Type parameter lists</h3>
25772578

25782579
<p>
25792580
A parsing ambiguity arises when the type parameter list for a generic type
2580-
declares a single type parameter with a type constraint of the form <code>*C</code>
2581-
or <code>(C)</code> where <code>C</code> is not a (possibly parenthesized)
2582-
<a href="#Types">type literal</a>:
2581+
declares a single type parameter <code>P</code> with a constraint <code>C</code>
2582+
such that the text <code>P C</code> forms a valid expression:
25832583
</p>
25842584

25852585
<pre>
25862586
type T[P *C] …
25872587
type T[P (C)] …
2588+
type T[P *C|Q] …
2589+
25882590
</pre>
25892591

25902592
<p>
2591-
In these rare cases, the type parameter declaration is indistinguishable from
2592-
the expressions <code>P*C</code> or <code>P(C)</code> and the type declaration
2593-
is parsed as an array type declaration.
2594-
To resolve the ambiguity, embed the constraint in an interface or use a trailing
2595-
comma:
2593+
In these rare cases, the type parameter list is indistinguishable from an
2594+
expression and the type declaration is parsed as an array type declaration.
2595+
To resolve the ambiguity, embed the constraint in an
2596+
<a href="#Interface_types">interface</a> or use a trailing comma:
25962597
</p>
25972598

25982599
<pre>
@@ -2606,6 +2607,11 @@ <h3 id="Type_parameter_lists">Type parameter lists</h3>
26062607
with a generic type.
26072608
</p>
26082609

2610+
<!--
2611+
This section needs to explain if and what kind of cycles are permitted
2612+
using type parameters in a type parameter list.
2613+
-->
2614+
26092615
<h4 id="Type_constraints">Type constraints</h4>
26102616

26112617
<p>
@@ -2625,10 +2631,10 @@ <h4 id="Type_constraints">Type constraints</h4>
26252631
</p>
26262632

26272633
<pre>
2628-
[T *P] // = [T interface{*P}]
2629-
[T ~int] // = [T interface{~int}]
2630-
[T int|string] // = [T interface{int|string}]
2631-
type Constraint ~int // illegal: ~int is not inside a type parameter list
2634+
[T []P] // = [T interface{[]P}]
2635+
[T ~int] // = [T interface{~int}]
2636+
[T int|string] // = [T interface{int|string}]
2637+
type Constraint ~int // illegal: ~int is not inside a type parameter list
26322638
</pre>
26332639

26342640
<!--
@@ -2821,7 +2827,7 @@ <h3 id="Function_declarations">Function declarations</h3>
28212827
</pre>
28222828

28232829
<p>
2824-
If the function declaration specifies <a href="#Type_parameter_lists">type parameters</a>,
2830+
If the function declaration specifies <a href="#Type_parameter_declarations">type parameters</a>,
28252831
the function name denotes a <i>generic function</i>.
28262832
A generic function must be <a href="#Instantiations">instantiated</a> before it can be
28272833
called or used as a value.
@@ -2911,13 +2917,10 @@ <h3 id="Method_declarations">Method declarations</h3>
29112917
If the receiver base type is a <a href="#Type_declarations">generic type</a>, the
29122918
receiver specification must declare corresponding type parameters for the method
29132919
to use. This makes the receiver type parameters available to the method.
2914-
</p>
2915-
2916-
<p>
29172920
Syntactically, this type parameter declaration looks like an
2918-
<a href="#Instantiations">instantiation</a> of the receiver base type, except that
2919-
the type arguments are the type parameters being declared, one for each type parameter
2920-
of the receiver base type.
2921+
<a href="#Instantiations">instantiation</a> of the receiver base type: the type
2922+
arguments must be identifiers denoting the type parameters being declared, one
2923+
for each type parameter of the receiver base type.
29212924
The type parameter names do not need to match their corresponding parameter names in the
29222925
receiver base type definition, and all non-blank parameter names must be unique in the
29232926
receiver parameter section and the method signature.
@@ -2931,8 +2934,8 @@ <h3 id="Method_declarations">Method declarations</h3>
29312934
b B
29322935
}
29332936

2934-
func (p Pair[A, B]) Swap() Pair[B, A] { return Pair[B, A]{p.b, p.a} }
2935-
func (p Pair[First, _]) First() First { return p.a }
2937+
func (p Pair[A, B]) Swap() Pair[B, A] { … } // receiver declares A, B
2938+
func (p Pair[First, _]) First() First { … } // receiver declares First, corresponds to A in Pair
29362939
</pre>
29372940

29382941
<h2 id="Expressions">Expressions</h2>
@@ -2974,7 +2977,7 @@ <h3 id="Operands">Operands</h3>
29742977

29752978
<p>
29762979
Implementation restriction: A compiler need not report an error if an operand's
2977-
type is a <a href="#Type_parameter_lists">type parameter</a> with an empty
2980+
type is a <a href="#Type_parameter_declarations">type parameter</a> with an empty
29782981
<a href="#Interface_types">type set</a>. Functions with such type parameters
29792982
cannot be <a href="#Instantiations">instantiated</a>; any attempt will lead
29802983
to an error at the instantiation site.
@@ -3759,7 +3762,7 @@ <h3 id="Index_expressions">Index expressions</h3>
37593762
</ul>
37603763

37613764
<p>
3762-
For <code>a</code> of <a href="#Type_parameter_lists">type parameter type</a> <code>P</code>:
3765+
For <code>a</code> of <a href="#Type_parameter_declarations">type parameter type</a> <code>P</code>:
37633766
</p>
37643767
<ul>
37653768
<li>The index expression <code>a[x]</code> must be valid for values
@@ -3952,7 +3955,7 @@ <h3 id="Type_assertions">Type assertions</h3>
39523955

39533956
<p>
39543957
For an expression <code>x</code> of <a href="#Interface_types">interface type</a>,
3955-
but not a <a href="#Type_parameter_lists">type parameter</a>, and a type <code>T</code>,
3958+
but not a <a href="#Type_parameter_declarations">type parameter</a>, and a type <code>T</code>,
39563959
the primary expression
39573960
</p>
39583961

@@ -4180,7 +4183,7 @@ <h3 id="Instantiations">Instantiations</h3>
41804183

41814184
<li>
41824185
After substitution, each type argument must <a href="#Interface_types">implement</a>
4183-
the <a href="#Type_parameter_lists">constraint</a> (instantiated, if necessary)
4186+
the <a href="#Type_parameter_declarations">constraint</a> (instantiated, if necessary)
41844187
of the corresponding type parameter. Otherwise instantiation fails.
41854188
</li>
41864189
</ol>
@@ -4193,9 +4196,9 @@ <h3 id="Instantiations">Instantiations</h3>
41934196
<pre>
41944197
type parameter list type arguments after substitution
41954198

4196-
[P any] int [int any]
4197-
[S ~[]E, E any] []int, int [[]int ~[]int, int any]
4198-
[P io.Writer] string [string io.Writer] // illegal: string doesn't implement io.Writer
4199+
[P any] int int implements any
4200+
[S ~[]E, E any] []int, int []int implements ~[]int, int implements any
4201+
[P io.Writer] string illegal: string doesn't implement io.Writer
41994202
</pre>
42004203

42014204
<p>
@@ -4259,7 +4262,7 @@ <h3 id="Type_inference">Type inference</h3>
42594262

42604263
<ul>
42614264
<li>
4262-
a <a href="#Type_parameter_lists">type parameter list</a>
4265+
a <a href="#Type_parameter_declarations">type parameter list</a>
42634266
</li>
42644267
<li>
42654268
a substitution map <i>M</i> initialized with the known type arguments, if any
@@ -4424,9 +4427,8 @@ <h4 id="Function_argument_type_inference">Function argument type inference</h4>
44244427
</p>
44254428

44264429
<p>
4427-
Function argument type inference can be used when the function has ordinary parameters
4428-
whose types are defined using the function's type parameters. Inference happens in two
4429-
separate phases; each phase operates on a specific list of (parameter, argument) pairs:
4430+
Inference happens in two separate phases; each phase operates on a specific list of
4431+
(parameter, argument) pairs:
44304432
</p>
44314433

44324434
<ol>
@@ -4778,7 +4780,7 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
47784780
</pre>
47794781

47804782
<p>
4781-
If the operand type is a <a href="#Type_parameter_lists">type parameter</a>,
4783+
If the operand type is a <a href="#Type_parameter_declarations">type parameter</a>,
47824784
the operator must apply to each type in that type set.
47834785
The operands are represented as values of the type argument that the type parameter
47844786
is <a href="#Instantiations">instantiated</a> with, and the operation is computed
@@ -5227,7 +5229,7 @@ <h3 id="Conversions">Conversions</h3>
52275229
</p>
52285230

52295231
<p>
5230-
Converting a constant to a type that is not a <a href="#Type_parameter_lists">type parameter</a>
5232+
Converting a constant to a type that is not a <a href="#Type_parameter_declarations">type parameter</a>
52315233
yields a typed constant.
52325234
</p>
52335235

@@ -5282,7 +5284,7 @@ <h3 id="Conversions">Conversions</h3>
52825284
<li>
52835285
ignoring struct tags (see below),
52845286
<code>x</code>'s type and <code>T</code> are not
5285-
<a href="#Type_parameter_lists">type parameters</a> but have
5287+
<a href="#Type_parameter_declarations">type parameters</a> but have
52865288
<a href="#Type_identity">identical</a> <a href="#Types">underlying types</a>.
52875289
</li>
52885290
<li>
@@ -6201,7 +6203,7 @@ <h4 id="Type_switches">Type switches</h4>
62016203
Cases then match actual types <code>T</code> against the dynamic type of the
62026204
expression <code>x</code>. As with type assertions, <code>x</code> must be of
62036205
<a href="#Interface_types">interface type</a>, but not a
6204-
<a href="#Type_parameter_lists">type parameter</a>, and each non-interface type
6206+
<a href="#Type_parameter_declarations">type parameter</a>, and each non-interface type
62056207
<code>T</code> listed in a case must implement the type of <code>x</code>.
62066208
The types listed in the cases of a type switch must all be
62076209
<a href="#Type_identity">different</a>.
@@ -6283,7 +6285,7 @@ <h4 id="Type_switches">Type switches</h4>
62836285
</pre>
62846286

62856287
<p>
6286-
A <a href="#Type_parameter_lists">type parameter</a> or a <a href="#Type_declarations">generic type</a>
6288+
A <a href="#Type_parameter_declarations">type parameter</a> or a <a href="#Type_declarations">generic type</a>
62876289
may be used as a type in a case. If upon <a href="#Instantiations">instantiation</a> that type turns
62886290
out to duplicate another entry in the switch, the first matching case is chosen.
62896291
</p>
@@ -7024,7 +7026,7 @@ <h3 id="Length_and_capacity">Length and capacity</h3>
70247026
</pre>
70257027

70267028
<p>
7027-
If the argument type is a <a href="#Type_parameter_lists">type parameter</a> <code>P</code>,
7029+
If the argument type is a <a href="#Type_parameter_declarations">type parameter</a> <code>P</code>,
70287030
the call <code>len(e)</code> (or <code>cap(e)</code> respectively) must be valid for
70297031
each type in <code>P</code>'s type set.
70307032
The result is the length (or capacity, respectively) of the argument whose type
@@ -7247,7 +7249,7 @@ <h3 id="Deletion_of_map_elements">Deletion of map elements</h3>
72477249
</pre>
72487250

72497251
<p>
7250-
If the type of <code>m</code> is a <a href="#Type_parameter_lists">type parameter</a>,
7252+
If the type of <code>m</code> is a <a href="#Type_parameter_declarations">type parameter</a>,
72517253
all types in that type set must be maps, and they must all have identical key types.
72527254
</p>
72537255

0 commit comments

Comments
 (0)