Home > Backend Development > Golang > How Can I Define Recursive Type Constraints in Go Generics Using Custom Interfaces?

How Can I Define Recursive Type Constraints in Go Generics Using Custom Interfaces?

Barbara Streisand
Release: 2025-01-04 09:28:34
Original
1022 people have browsed it

How Can I Define Recursive Type Constraints in Go Generics Using Custom Interfaces?

Defining Recursive Type Constraints with Custom Interfaces

In Go generics, type constraints can be defined using interfaces. However, the standard draft does not allow for recursive type constraints, where a type's method has an argument of the generic type.

Problem Scenario

Consider the following code that defines an interface Lesser and a function IsLess:

type Lesser interface {
    Less(rhs Lesser) bool
}

func IsLess[T Lesser](lhs, rhs T) bool {
    return lhs.Less(rhs)
}
Copy after login

Running this code results in an error:

Int does not satisfy Lesser: wrong method signature
    got  func (Int).Less(rhs Int) bool
    want func (Lesser).Less(rhs Lesser) bool
Copy after login

Solution

To address this issue, we can define the Lesser interface and the IsLess function as follows:

type Lesser[T any] interface {
    Less(T) bool
}

func IsLess[T Lesser[T]](x, y T) bool {
    return x.Less(y)
}
Copy after login

In this definition, the Lesser interface takes a type parameter T and declares a method Less that accepts an argument of type T. The IsLess function constrains the generic type T to implement the Lesser[T] interface.

Example Usage

Here is an example of using this solution:

type Apple int

func (a Apple) Less(other Apple) bool {
    return a < other
}

func main() {
    fmt.Println(IsLess(Apple(10), Apple(20)))   // true
    fmt.Println(IsLess(Apple(20), Orange(30))) // compilation error: type mismatch
}
Copy after login

In this example, the Apple type satisfies the Lesser[Apple] interface because its Less method accepts an argument of type Apple. The IsLess function can then be used to compare two Apple values.

This approach allows for recursive type constraints, enabling you to enforce that a type's method has an argument of the generic type.

The above is the detailed content of How Can I Define Recursive Type Constraints in Go Generics Using Custom Interfaces?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template