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

Go error: Cannot use generic type without instantiation

王林
Release: 2024-02-08 23:12:09
forward
727 people have browsed it

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])
}
Copy after login

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
Copy after login

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)
Copy after login

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} }
Copy after login

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 }
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template