


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

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

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 difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...

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, ...
