The father of the Go language introduces generics

The official Go blog recently published an article about the new feature "generics" by two heavyweights - Robert Griesemer and Ian Lance Taylor, based on their talk at GopherCon 2021.

▲ Left: Robert Griesemer, one of the designers of the Go language; Right: Ian Lance Taylor, the main designer of Go generics

Video address: https://www.youtube.com/watch?v=Pa_e9EeCdy8&t=54s

Go 1.18 , which was officially released not long ago, added support for generics, which is said to be the biggest change since Go was open sourced. Generics is a programming paradigm that is independent of the specific type used, allowing the use of any type from a collection of types in the implementation of functions and types.

Generics add three new important things to Go:

  1. "type parameters" for functions and types
  2. Defines an interface type as a collection of types, including interface types without methods
  3. Type inference: In most cases, " type arguments " can be omitted when calling a generic function

Type Parameters

Now both functions and types have type parameters", and the type parameter list looks like a normal parameter list, except that it uses square brackets instead of parentheses.

Let's start with the basic non-generic Min function for floating point values:

func Min(x, y float64) float64 {
    if x < y {
        return x
    }
    return y
}

Make this function generic by adding a type parameter list - making it work for different types. In this example, a type parameter list with a single type parameter is added Tand replaced float64.

import "golang.org/x/exp/constraints"

func GMin[T constraints.Ordered](x, y T) T {
    if x < y {
        return x
    }
    return y
}

This function can then be called with the type arguments:

x := GMin[int](2, 3)

Type parameters are provided to GMin, in this case intcalled instantiation. Instantiation occurs in two steps. First, the compiler replaces all type parameters with their respective type arguments in a generic function or generic type. The compiler then verifies that each type parameter satisfies the respective constraints. If the second step fails, the instantiation fails and the program is invalid.

Upon successful instantiation, a non-generic function is produced, which can be called like any other function. for example:

fmin := GMin[float64]
m := fmin(2.71, 3.14)

GMin[float64]The instantiation of yields a Minfunction equivalent that can be used in function calls. Type parameters can also be used with types.

type Tree[T interface{}] struct {
    left, right *Tree[T]
    value       T
}

func (t *Tree[T]) Lookup(x T) *Tree[T] { ... }

var stringTree Tree[string]

In the above example, the generic typeTree stores the value of the type parameter T. Generic types can also have methods, like in this example Lookup. In order to use a generic type, it must be instantiated; is an example of instantiation Tree[string]using type arguments .stringTree

See the original text for more introduction .

Guess you like

Origin www.oschina.net/news/188448/intro-go-generics