Google’s Go language could add generics later this year

Google Logo
Image: IDGNS

Proposal to add generic programming using type parameters the latest attempt to make language easier to use

Print

PrintPrint
Pro

Read More:

14 January 2021 | 0

Google’s Go finally could be adding generics, long sought by many Go users as a mechanism to simplify the language.

A Go language change proposal filed 12 January in GitHub calls for adding support for type parameters for types and functions, thus enabling a form of generic programming. Efforts to add generics to Go have been going on for years, with support for generics being one of the most-commonly requested features since Go was first released in 2009. Now, Go developers may see an implementation by the end of this year, perhaps included as part of Go 1.18 beta releases. The implementation would be complete but perhaps not fully optimised.

Generics can provide powerful building blocks to share code and more easily build programs. With generic programming, writing functions and data structures can be done in a manner where some types are specified afterward. For example, a developer could write a function that operates on a slice of an arbitrary data type, where the actual data type is specified when the function is called. A developer also could define a data structure that stores values of any type, in which the actual type to be stored is specified when an instance of the data structure is created.

 

advertisement



 

High-level changes in the generic programming proposal for Go include:

  • 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 allows omitting the type arguments of a function call in common cases.

Fitting generics into a language such as Go is a difficult task, as failed attempts dating back to 2010 indicate. In the past couple of years, the developers of Go have worked on a series of design drafts that culminated in a design based on type parameters. The draft has had input from the Go programming community, and there has been some experimentation with it via the generics playground.

The changes to the language anticipated for generics support are backward-compatible, so existing Go programs would keep working. The current version of Go is version 1.15, with Go 1.16 now in a beta stage. A production release of Go 1.16 is eyed for next month.

IDG News Service

Find out more


Read More:



Comments are closed.

Back to Top ↑