From a287c4aa38c9c71f823a0c366871f7f4452a602c Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 18 Nov 2021 15:33:19 -0800 Subject: [PATCH] spec: add type parameter types - add section on type parameters - added two sections on the scope of type parameters - expanded general section on types accordingly - introduced the notion of a named type which will help in simplifying various rules (subsequent CLs) Change-Id: I49c1ed7d6d4f951d751f0a3ca5dfb637e49829f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/365414 Trust: Robert Griesemer Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 62 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 0ce6a3ca18..2120985b3b 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -787,32 +787,46 @@ If a variable has not yet been assigned a value, its value is the

A type determines a set of values together with operations and methods specific -to those values. A type may be denoted by a type name, if it has one, -or specified using a type literal, which composes a type from existing types. +to those values. A type may be denoted by a type name, if it has one, which must be +followed by type arguments if the type is parameterized. +A type may also be specified using a type literal, which composes a type +from existing types.

-Type      = TypeName | TypeLit | "(" Type ")" .
+Type      = TypeName [ TypeArgs ] | TypeLit | "(" Type ")" .
 TypeName  = identifier | QualifiedIdent .
+TypeArgs  = "[" TypeList [ "," ] "]" .
+TypeList  = Type { "," Type } .
 TypeLit   = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
 	    SliceType | MapType | ChannelType .
 

The language predeclares certain type names. -Others are introduced with type declarations. +Others are introduced with type declarations +or type parameter lists. Composite types—array, struct, pointer, function, interface, slice, map, and channel types—may be constructed using type literals.

+

+Predeclared types, defined types, and type parameters are called named types. +An alias denotes a named type if the type given in the alias declaration is a named type. +

+ +

Underlying types

+

Each type T has an underlying type: If T is one of the predeclared boolean, numeric, or string types, or a type literal, -the corresponding underlying -type is T itself. Otherwise, T's underlying type -is the underlying type of the type to which T refers in its -type declaration. +the corresponding underlying type is T itself. +Otherwise, T's underlying type is the underlying type of the +type to which T refers in its type +declaration. Accordingly, the underlying type of a type parameter is the +underlying type of its type constraint, which +is always an interface.

@@ -827,12 +841,15 @@ type (
 	B3 []B1
 	B4 B3
 )
+
+func f[P any](x P) { … }
 

The underlying type of string, A1, A2, B1, and B2 is string. The underlying type of []B1, B3, and B4 is []B1. +The underlying type of P is interface{}.

Method sets

@@ -1706,6 +1723,25 @@ and a second goroutine receives them, the values are received in the order sent.

+

Type parameters

+ +

+A type parameter is an (unqualified) type name declared in the +type parameter list of a +function declaration or +type definition; or in the receiver specification +of a method declaration that is associated +with a parameterized type. +A type parameter acts as a place holder for an (as of yet) unknown type in the declaration; +the type parameter is replaced with a type argument upon +instantiation of the parameterized function or type. +

+ +

+The properties of a type parameter are determined by its +type constraint. +

+

Properties of types and values

Type identity

@@ -1983,6 +2019,15 @@ Go is lexically scoped using blocks:
  • The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
  • +
  • The scope of an identifier denoting a type parameter of a type-parameterized function + or declared by a method receiver is the function body and all parameter lists of the + function. +
  • + +
  • The scope of an identifier denoting a type parameter of a parameterized type + begins after the name of the parameterized type and ends at the end + of the TypeSpec.
  • +
  • The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) @@ -5384,7 +5429,6 @@ TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClau TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" . TypeCaseClause = TypeSwitchCase ":" StatementList . TypeSwitchCase = "case" TypeList | "default" . -TypeList = Type { "," Type } .

    -- 2.50.0