Home  >  Article  >  Backend Development  >  Generics: Constraints and structures embedded in interfaces

Generics: Constraints and structures embedded in interfaces

王林
王林forward
2024-02-08 23:21:19572browse

Generics: Constraints and structures embedded in interfaces

php editor Xiaoxin brings you an introduction to generics. Generics are constraints and structures embedded in interfaces that allow us to be more flexible and efficient when writing code. By using generics, we can find and resolve type errors at compile time, improving code readability and maintainability. Generics can also help us design more general data structures and algorithms, improving code reusability. In this article, we will delve into the concepts, usage, and practical applications of generics to help you better understand and apply generic programming.

Problem content

The following code cannot be compiled and reports./main.go:35:7: test does not meet constraint (test missing in main.structwithname)

package main

import "fmt"

type Test struct {
    Name string
}

func (t Test) String() string {
    return fmt.Sprintf("Name: %v", t.Name)
}

type StructWithName struct {
    Name string
}

type Constraint interface {
    StructWithName
    // ~struct {
    //  Name string
    // }
    String() string
}

func Print[T Constraint](x T) {
    //s := T{}
    fmt.Printf("Hello %v", x)
}

func main() {
    t := Test{
        Name: "Test",
    }

    Print(t)
}

However, if I comment out the structwithname and uncomment the ~struct block, then it compiles fine. I can't understand the semantic difference between the two.

What I'm trying to do is have a generic method that can handle a struct in a generic way; i.e. x := s{} and then pass it to an external library. In this case it's for the terraform provider, the code in the resources is very similar, I'm a bit bothered by the amount of duplication I see in the existing providers; in my case the resources are very similar, some are in between the default structures There are only one or two fields on it). I'd like to be able to write a set of generic methods that do all similar things, and only have the specificities elsewhere (duh!).

I'm still in the early stages of my go journey and I can't understand the compiler errors.

Solution

If I understand correctly, what you really want is not possible. Go generics do not support accessing a subset of structure fields.

As far as the errors involved...

The name of a type specifies the identity of the type, and two types with different names (not aliases to each other) are always different, so test and structwithname are two different type. The only thing they have in common is their underlying type.

type constraint interface { structwithname } The interface is a type set that contains only one type, that is, structwithname, and since test is not of this type set member, which means it is not possible to use the test type to satisfy the constraint.

type struct { name string } is an unnamed type. The underlying type of any unnamed type is the type itself, that is, the underlying type of struct { name string } is struct { name string }.

The tilde in front of the type in the constraint, that is, ~t, means any type, whose underlying type is the same as t. Or, more accurately,

The type set of an item of the form

~t is the set of all types whose base type is t.

So when you do this

type Constraint interface { ~struct { Name string } }

Then the constrained type set will contain any type that has struct { name string } as its base type.

The above is the detailed content of Generics: Constraints and structures embedded in interfaces. 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