Description
We propose adding support for type parameters to Go. This will change the Go language to support a form of generic programming.
A detailed proposal document has been published, with input from many members of the Go community. We are now taking the next step and proposing that this document become a part of the language.
A very high level overview of the proposed changes:
- Functions can have an additional type parameter list that uses square brackets but otherwise looks like an ordinary parameter list:
func F[T any](p T) { ... }
. - These type parameters can be used by the regular parameters and in the function body.
- Types can also have a type parameter list:
type MySlice[T any] []T
. - Each type parameter has a type constraint, just as each ordinary parameter has a type:
func F[T Constraint](p T) { ... }
. - Type constraints are interface types.
- The new predeclared name
any
is a type constraint that permits any type. - Interface types used as type constraints can have a list of predeclared types; only type arguments that match one of those types satisfy the constraint.
- Generic functions may only use operations permitted by their type constraints.
- Using a generic function or type requires passing type arguments.
- Type inference permits omitting the type arguments of a function call in common cases.
For more background on this proposal, see the recent blog post.
In the discussion on this issue, we invite substantive criticisms and comments, but please try to avoid repeating earlier comments, and please try to avoid simple plus-one and minus-one comments. Instead, add thumbs-up/thumbs-down emoji reactions to comments with which you agree or disagree, or to the proposal as a whole.
If you don't understand parts of the design please consider asking questions in a forum, rather than on this issue, to keep the discussion here more focused. See https://golang.org/wiki/Questions.