Home  >  Article  >  Backend Development  >  Go: How to specify a type constraint where the method's argument type is the same as the receiver's type

Go: How to specify a type constraint where the method's argument type is the same as the receiver's type

王林
王林forward
2024-02-09 16:33:08503browse

Go: How to specify a type constraint where the methods argument type is the same as the receivers type

In the Go language, we can use type constraints to specify the parameter types of functions or methods. When we want the parameter type of a method to be the same as the receiver type, how do we specify it? First of all, it needs to be clear that the Go language does not directly support the feature that the parameter type is the same as the receiver type. However, we can achieve a similar effect by using pointer types in method definitions. Next, we will detail how to specify the parameter type to be the same as the receiver type in Go language.

Question content

I want to specify a type constraint as shown below:

type Comparer interface {
    Compare(another Comparer) int
}

But I want the implementation type to pass its own concrete type into the method Compare instead of the interface Comparer as shown below (I know the following does not implement Comparer):

func (a MyInt) Compare(b MyInt) int {
    xxxx
    return xxxx
}

I try to use a generic interface like this:

type Comparer[T any] interface {
    Compare(T) int
}

But this does not force the receiver of method Compare to also be of type T.

Is there a way to force the receiver type and parameter type of method Compare to be the same?

Workaround

When you talk about constraints, you are essentially referring to a specific usage of the interface type as a restriction on the set of type parameters.

So when you (correctly) define the interface as:

type Comparer[T any] interface {
    Compare(T) int
}

You only tell half the story. In fact, the above is not a limitation. It's just an interface.

In order to truly be a type constraint, the interface must be used as a.

func Foo[T Comparer[T]](t1, t2 T) int {
    return t1.Compare(t2)
}

type Thing[T Comparer[T]] struct {
    Value T
}

Only in a type parameter list, you can force the receiver of Compare(T) to be T itself by instantiating the constraint with its type parameter.

When not used as a constraint, an interface is simply a definition of a set of methods, by design without any restrictions on the types that can implement it.

You can now use type terms to specify which types must implement a certain interface. But type parameters cannot be used directly as type terms. You must use an unnamed type, such as a pointer to T:

type Comparer[T any] interface {
    *T
    Compare(T) int
}

Note that this forces you to declare the method on the pointer receiver, such as *MyInt, which may or may not be ideal.

Regardless, this cannot be instantiated with its own type parameter as T Comparer[T] because the constraint imposes an additional level of pointer indirection no matter what T is. Function parameters never satisfy it.

The trick to implementing this functionality is to instantiate Comparer with different type parameters.

func test[T any, V Comparer[T]](a, b T) int {
    return V(&a).Compare(b)
}

and declare the method as:

type MyInt int

func (t *MyInt) Compare(other MyInt) int {
    // implementation
}

Though if you use interface constraints as expected, this complicated workaround becomes completely unnecessary.

Playgroundhttps://www.php.cn/link/3ea816621e0d8ecd5e534ec28051d4d5

The above is the detailed content of Go: How to specify a type constraint where the method's argument type is the same as the receiver's type. 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