


Go: How to specify a type constraint where the method's argument type is the same as the receiver's 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Using Golang to implement Linux...

Regarding the problem of custom structure tags in Goland When using Goland for Go language development, you often encounter some configuration problems. One of them is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Performance optimization strategy for Go language massive URL access This article proposes a performance optimization solution for the problem of using Go language to process massive URL access. Existing programs from CSV...
