若一个类型指定了[类型参数],则此类型成为泛型类型。
type List[T any] struct {
next *List[T]
value T
}
类型参数
类型参数的语法如下:
TypeParameters = "[" TypeParamList [ "," ] "]" .
TypeParamList = TypeParamDecl { "," TypeParamDecl } .
TypeParamDecl = IdentifierList TypeConstraint .
其中 TypeConstraint
即为:
TypeConstraint
TypeElem = TypeTerm { "|" TypeTerm } .
TypeTerm = Type | UnderlyingType .
UnderlyingType = "~" Type .