From: Robert Griesemer
The language predeclares certain type names.
Others are introduced with type declarations
-or type parameter lists.
+or type parameter lists.
Composite types—array, struct, pointer, function,
interface, slice, map, and channel types—may be constructed using
type literals.
@@ -1459,7 +1459,7 @@ type Float interface {
-In a union, a term cannot be a type parameter, and the type sets of all
+In a union, a term cannot be a type parameter, and the type sets of all
non-interface terms must be pairwise disjoint (the pairwise intersection of the type sets must be empty).
Given a type parameter
By definition, a core type is never a defined type,
-type parameter, or
+type parameter, or
interface type.
A constant
-If the type definition specifies type parameters,
+If the type definition specifies type parameters,
the type name denotes a generic type.
Generic types must be instantiated when they
are used.
@@ -2538,7 +2539,7 @@ present in the generic type definition.
func (l *List[T]) Len() int { ⦠}
-
A type parameter list declares the type parameters of a generic function or type declaration.
@@ -2577,22 +2578,22 @@ has a corresponding (meta-)type which is called its
A parsing ambiguity arises when the type parameter list for a generic type
-declares a single type parameter with a type constraint of the form
-In these rare cases, the type parameter declaration is indistinguishable from
-the expressions P
:
T
.
x
is representable
by a value of type T
,
-where T
is not a type parameter,
+where T
is not a type parameter,
if one of the following conditions applies:
Type parameter lists
+Type parameter declarations
*C
-or (C)
where C
is not a (possibly parenthesized)
-type literal:
+declares a single type parameter P
with a constraint C
+such that the text P C
forms a valid expression:
type T[P *C] â¦
type T[P (C)] â¦
+type T[P *C|Q] â¦
+â¦
P*C
or P(C)
and the type declaration
-is parsed as an array type declaration.
-To resolve the ambiguity, embed the constraint in an interface or use a trailing
-comma:
+In these rare cases, the type parameter list is indistinguishable from an
+expression and the type declaration is parsed as an array type declaration.
+To resolve the ambiguity, embed the constraint in an
+interface or use a trailing comma:
@@ -2606,6 +2607,11 @@ of a method declaration associated
with a generic type.
@@ -2625,10 +2631,10 @@ the enclosing interface{ ⦠}
may be omitted for convenience:
-[T *P] // = [T interface{*P}] -[T ~int] // = [T interface{~int}] -[T int|string] // = [T interface{int|string}] -type Constraint ~int // illegal: ~int is not inside a type parameter list +[T []P] // = [T interface{[]P}] +[T ~int] // = [T interface{~int}] +[T int|string] // = [T interface{int|string}] +type Constraint ~int // illegal: ~int is not inside a type parameter list