Home  >  Article  >  Backend Development  >  Is there a way to ensure that the passed value has certain fields using generics?

Is there a way to ensure that the passed value has certain fields using generics?

王林
王林forward
2024-02-10 16:27:22650browse

Is there a way to ensure that the passed value has certain fields using generics?

Is there a way to ensure that the value passed has certain fields using generics? This is a problem that many developers often encounter when using generics. In PHP, we can achieve this function through type constraints and interface implementation. First, we can use type constraints to ensure that the passed value is an instance of a class. We can then use the interface to define specifications for certain fields, ensuring that the passed value has these fields. This way, we can use generics in our code and ensure that the passed values ​​meet specific field requirements. In this way, we can better take advantage of generics and ensure the reliability and security of our code.

Question content

I am trying to define a generic function in go that accepts a value with some fields, such as id int. I've tried several methods but none seem to work. Here's an example of what I've tried.

package main

import (
    "fmt"
)

func Print[T IDer](s T) {
    fmt.Print(s.ID)
}

func main() {
    Print(Person{3, "Test"})
}

type IDer interface {
    ~struct{ ID int }
}

type Person struct {
    ID   int
    Name string
}

type Store struct {
    ID     int
    Domain string
}

This is the playground link: https://gotipplay.golang.org/p/2i4rsucwagf

In the above example, I want to ensure that each value passed to the print function has a property id int that can also be accessed within the function. Is there any way to achieve this in go without defining the method in the interface (e.g. getid() int)?

Solution

Is there any way to achieve this in go without defining a method in the interface (e.g. getid() int)?

No, you must define the method in the interface.

The generics implementation in go 1.18 does not support struct types, although the primitive type parameter proposal suggests support. To access public fields in a union, see also this explanation.

Nonetheless, I think it's worth pointing out a misunderstanding that could easily arise from your example: the meaning of the approximation ~t (tilde type) means "the set of types whose underlying type is t. p>

Now, when you write:

~struct{ id int }

This means that its underlying type is exactly struct{ id int }. Anyway, this does not include structs with fields id int and other stuff. For example. type foo struct { underlying type of id int; named string} is struct { id int; named string}, not struct{ id int }, so the constraints cannot be satisfied anyway.

The current time parameter implementation does not have the syntax to specify some structure types. I remember ProposalAdd field terms (and type terms and methods) in interface constraints), it went online:

type IDer interface {
    ID int
}

This will achieve what you want to do without destroying the meaning of the tilde ~. But this won't be included in go 1.18.

The above is the detailed content of Is there a way to ensure that the passed value has certain fields using generics?. 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