Table of Contents
Question content
Workaround
Home Backend Development Golang 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

Feb 09, 2024 pm 04:33 PM
go language

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
}
Copy after login

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
}
Copy after login

I try to use a generic interface like this:

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

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
}
Copy after login
Copy after login

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
}
Copy after login

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
}
Copy after login

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)
}
Copy after login

and declare the method as:

type MyInt int

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

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

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

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

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 provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

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

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

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

How to implement operations on Linux iptables linked lists in Golang? How to implement operations on Linux iptables linked lists in Golang? Apr 02, 2025 am 10:18 AM

Using Golang to implement Linux...

How to solve the problem that custom structure labels in Goland do not take effect? How to solve the problem that custom structure labels in Goland do not take effect? Apr 02, 2025 pm 12:51 PM

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

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

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

Go language is inefficient in processing massive URL access, how to optimize it? Go language is inefficient in processing massive URL access, how to optimize it? Apr 02, 2025 am 10:15 AM

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

See all articles