Home >Backend Development >Golang >How Can I Define Recursive Type Constraints in Go Generics Using 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.
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) }
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
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) }
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.
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 }
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!