Home  >  Article  >  Backend Development  >  Go error: Cannot use generic type without instantiation

Go error: Cannot use generic type without instantiation

王林
王林forward
2024-02-08 23:12:09691browse

Go error: Cannot use generic type without instantiation

What php editor Apple wants to share with you today is about a bug in the Go language: generic types cannot be used without instantiation. In the Go language, generics are a very powerful feature that allows us to write more versatile and flexible code. However, sometimes we may encounter a problem where we cannot use a generic type without instantiation. This error may leave us confused as to how to fix it. Next, let us take a look at the causes and solutions to this problem!

Question content

While learning Go generics, I encountered an error that I couldn't seem to solve. I boiled it down to the simplest code:

type opStack[T any] []T

func main() {

    t := make(opStack)
    //  t := new(opStack)
    t = append(t, 0)
    fmt.Println(t[0])
}

In the Playground, the following error message appears on the make() call (and similarly on the commented out new call):

cannot use generic type opStack[T any] without instantiation

But make() is an instantiation function. So, I'm hoping I'm missing some syntax subtlety. What is Go complaining about and what needs to be corrected?

Workaround

Whenever you use a parameterized type, including anywhere that requires a type parameter, such as the built-in make, you must Replace the type parameters in its definition with actual types. This is called instantiation.

t := make(opStack[int], 0)
t = append(t, 0)

If a generic type is used as a type parameter of another generic type, it must also be instantiated:

type Data[T any] struct {
    data T
}

d := Data[opStack[int]]{ data: []int{0, 1, 2} }

You can use type parameters for instantiation, for example in function signatures, fields and type definitions:

type FooBar[T any] struct {
    ops opStack[T]
}

type OpsMap[T any] map[string]opStack[T]

func echo[T any](ops opStack[T]) opStack[T] { return ops }

The relevant references in the language specification are (currently) in two different places, type definitions一个>:

and Instantiation

In other programming languages, "instantiation" may refer to creating an instance of an object - in Go, the term specifically refers to replacing type parameters with concrete types. In my opinion, the usage of the term is still consistent, although in Go it doesn't necessarily mean allocation.

Note that you can call a generic function without explicit type parameters. Instantiation happens there too, except that the type parameters may all be inferred from the function parameters:

func Print[T, U any](v T, w U) { /* ... */ }

Print("foo", 4.5) // T is inferred from "foo", U from 4.5

Reasoning used to apply to generic types as well, with the restriction that the type parameter list must be non-empty. But this feature is disabled, so you must provide all type parameters explicitly.

type Vector[T any] []T 
// v := Vector[int]{} -> must supply T

type Matrix[T any, U ~[]T] []U 
// m := Matrix[int, []int]{} -> must supply T and U

The above is the detailed content of Go error: Cannot use generic type without instantiation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete