@@ -807,7 +807,7 @@ <h2 id="Types">Types</h2>
807
807
< p >
808
808
The language < a href ="#Predeclared_identifiers "> predeclares</ a > certain type names.
809
809
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 > .
811
811
< i > Composite types</ i > —array, struct, pointer, function,
812
812
interface, slice, map, and channel types—may be constructed using
813
813
type literals.
@@ -1459,7 +1459,7 @@ <h4 id="General_interfaces">General interfaces</h4>
1459
1459
</ pre >
1460
1460
1461
1461
< 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
1463
1463
non-interface terms must be pairwise disjoint (the pairwise intersection of the type sets must be empty).
1464
1464
Given a type parameter < code > P</ code > :
1465
1465
</ p >
@@ -1769,7 +1769,7 @@ <h3 id="Core_types">Core types</h3>
1769
1769
1770
1770
< p >
1771
1771
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
1773
1773
< a href ="#Interface_types "> interface type</ a > .
1774
1774
</ p >
1775
1775
@@ -1965,7 +1965,7 @@ <h3 id="Representability">Representability</h3>
1965
1965
< p >
1966
1966
A < a href ="#Constants "> constant</ a > < code > x</ code > is < i > representable</ i >
1967
1967
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 > ,
1969
1969
if one of the following conditions applies:
1970
1970
</ p >
1971
1971
@@ -2105,6 +2105,7 @@ <h2 id="Declarations_and_scope">Declarations and scope</h2>
2105
2105
A < i > declaration</ i > binds a non-< a href ="#Blank_identifier "> blank</ a > identifier to a
2106
2106
< a href ="#Constant_declarations "> constant</ a > ,
2107
2107
< a href ="#Type_declarations "> type</ a > ,
2108
+ < a href ="#Type_parameter_declarations "> type parameter</ a > ,
2108
2109
< a href ="#Variable_declarations "> variable</ a > ,
2109
2110
< a href ="#Function_declarations "> function</ a > ,
2110
2111
< a href ="#Labeled_statements "> label</ a > , or
@@ -2502,7 +2503,7 @@ <h4 id="Type_definitions">Type definitions</h4>
2502
2503
</ pre >
2503
2504
2504
2505
< 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 > ,
2506
2507
the type name denotes a < i > generic type</ i > .
2507
2508
Generic types must be < a href ="#Instantiations "> instantiated</ a > when they
2508
2509
are used.
@@ -2538,7 +2539,7 @@ <h4 id="Type_definitions">Type definitions</h4>
2538
2539
func (l *List[T]) Len() int { … }
2539
2540
</ pre >
2540
2541
2541
- < h3 id ="Type_parameter_lists "> Type parameter lists </ h3 >
2542
+ < h3 id ="Type_parameter_declarations "> Type parameter declarations </ h3 >
2542
2543
2543
2544
< p >
2544
2545
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>
2577
2578
2578
2579
< p >
2579
2580
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:
2583
2583
</ p >
2584
2584
2585
2585
< pre >
2586
2586
type T[P *C] …
2587
2587
type T[P (C)] …
2588
+ type T[P *C|Q] …
2589
+ …
2588
2590
</ pre >
2589
2591
2590
2592
< 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:
2596
2597
</ p >
2597
2598
2598
2599
< pre >
@@ -2606,6 +2607,11 @@ <h3 id="Type_parameter_lists">Type parameter lists</h3>
2606
2607
with a generic type.
2607
2608
</ p >
2608
2609
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
+
2609
2615
< h4 id ="Type_constraints "> Type constraints</ h4 >
2610
2616
2611
2617
< p >
@@ -2625,10 +2631,10 @@ <h4 id="Type_constraints">Type constraints</h4>
2625
2631
</ p >
2626
2632
2627
2633
< 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
2632
2638
</ pre >
2633
2639
2634
2640
<!--
@@ -2821,7 +2827,7 @@ <h3 id="Function_declarations">Function declarations</h3>
2821
2827
</ pre >
2822
2828
2823
2829
< 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 > ,
2825
2831
the function name denotes a < i > generic function</ i > .
2826
2832
A generic function must be < a href ="#Instantiations "> instantiated</ a > before it can be
2827
2833
called or used as a value.
@@ -2911,13 +2917,10 @@ <h3 id="Method_declarations">Method declarations</h3>
2911
2917
If the receiver base type is a < a href ="#Type_declarations "> generic type</ a > , the
2912
2918
receiver specification must declare corresponding type parameters for the method
2913
2919
to use. This makes the receiver type parameters available to the method.
2914
- </ p >
2915
-
2916
- < p >
2917
2920
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.
2921
2924
The type parameter names do not need to match their corresponding parameter names in the
2922
2925
receiver base type definition, and all non-blank parameter names must be unique in the
2923
2926
receiver parameter section and the method signature.
@@ -2931,8 +2934,8 @@ <h3 id="Method_declarations">Method declarations</h3>
2931
2934
b B
2932
2935
}
2933
2936
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
2936
2939
</ pre >
2937
2940
2938
2941
< h2 id ="Expressions "> Expressions</ h2 >
@@ -2974,7 +2977,7 @@ <h3 id="Operands">Operands</h3>
2974
2977
2975
2978
< p >
2976
2979
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
2978
2981
< a href ="#Interface_types "> type set</ a > . Functions with such type parameters
2979
2982
cannot be < a href ="#Instantiations "> instantiated</ a > ; any attempt will lead
2980
2983
to an error at the instantiation site.
@@ -3759,7 +3762,7 @@ <h3 id="Index_expressions">Index expressions</h3>
3759
3762
</ ul >
3760
3763
3761
3764
< 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 > :
3763
3766
</ p >
3764
3767
< ul >
3765
3768
< li > The index expression < code > a[x]</ code > must be valid for values
@@ -3952,7 +3955,7 @@ <h3 id="Type_assertions">Type assertions</h3>
3952
3955
3953
3956
< p >
3954
3957
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 > ,
3956
3959
the primary expression
3957
3960
</ p >
3958
3961
@@ -4180,7 +4183,7 @@ <h3 id="Instantiations">Instantiations</h3>
4180
4183
4181
4184
< li >
4182
4185
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)
4184
4187
of the corresponding type parameter. Otherwise instantiation fails.
4185
4188
</ li >
4186
4189
</ ol >
@@ -4193,9 +4196,9 @@ <h3 id="Instantiations">Instantiations</h3>
4193
4196
< pre >
4194
4197
type parameter list type arguments after substitution
4195
4198
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
4199
4202
</ pre >
4200
4203
4201
4204
< p >
@@ -4259,7 +4262,7 @@ <h3 id="Type_inference">Type inference</h3>
4259
4262
4260
4263
< ul >
4261
4264
< li >
4262
- a < a href ="#Type_parameter_lists "> type parameter list</ a >
4265
+ a < a href ="#Type_parameter_declarations "> type parameter list</ a >
4263
4266
</ li >
4264
4267
< li >
4265
4268
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>
4424
4427
</ p >
4425
4428
4426
4429
< 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:
4430
4432
</ p >
4431
4433
4432
4434
< ol >
@@ -4778,7 +4780,7 @@ <h3 id="Arithmetic_operators">Arithmetic operators</h3>
4778
4780
</ pre >
4779
4781
4780
4782
< 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 > ,
4782
4784
the operator must apply to each type in that type set.
4783
4785
The operands are represented as values of the type argument that the type parameter
4784
4786
is < a href ="#Instantiations "> instantiated</ a > with, and the operation is computed
@@ -5227,7 +5229,7 @@ <h3 id="Conversions">Conversions</h3>
5227
5229
</ p >
5228
5230
5229
5231
< 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 >
5231
5233
yields a typed constant.
5232
5234
</ p >
5233
5235
@@ -5282,7 +5284,7 @@ <h3 id="Conversions">Conversions</h3>
5282
5284
< li >
5283
5285
ignoring struct tags (see below),
5284
5286
< 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
5286
5288
< a href ="#Type_identity "> identical</ a > < a href ="#Types "> underlying types</ a > .
5287
5289
</ li >
5288
5290
< li >
@@ -6201,7 +6203,7 @@ <h4 id="Type_switches">Type switches</h4>
6201
6203
Cases then match actual types < code > T</ code > against the dynamic type of the
6202
6204
expression < code > x</ code > . As with type assertions, < code > x</ code > must be of
6203
6205
< 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
6205
6207
< code > T</ code > listed in a case must implement the type of < code > x</ code > .
6206
6208
The types listed in the cases of a type switch must all be
6207
6209
< a href ="#Type_identity "> different</ a > .
@@ -6283,7 +6285,7 @@ <h4 id="Type_switches">Type switches</h4>
6283
6285
</ pre >
6284
6286
6285
6287
< 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 >
6287
6289
may be used as a type in a case. If upon < a href ="#Instantiations "> instantiation</ a > that type turns
6288
6290
out to duplicate another entry in the switch, the first matching case is chosen.
6289
6291
</ p >
@@ -7024,7 +7026,7 @@ <h3 id="Length_and_capacity">Length and capacity</h3>
7024
7026
</ pre >
7025
7027
7026
7028
< 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 > ,
7028
7030
the call < code > len(e)</ code > (or < code > cap(e)</ code > respectively) must be valid for
7029
7031
each type in < code > P</ code > 's type set.
7030
7032
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>
7247
7249
</ pre >
7248
7250
7249
7251
< 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 > ,
7251
7253
all types in that type set must be maps, and they must all have identical key types.
7252
7254
</ p >
7253
7255
0 commit comments