使用定义类型而不是类型文字的递归类型约束?

在 Go2 泛型中,截至目前的草案,我可以使用接口在泛型类型上指定类型约束。


import "fmt"


type Stringer interface {

    String() string

}


func Print[T Stringer](value T) {

    fmt.Println(value.String())

}

这样,我可以指定该类型必须实现一个方法。但是,我看不到任何强制实现方法的方法,因为它本身具有泛型类型的参数。


type Lesser interface {

    Less(rhs Lesser) bool

}


type Int int


func (lhs Int) Less(rhs Int) bool {

    return lhs < rhs

}


func IsLess[T Lesser](lhs, rhs T) bool {

    return lhs.Less(rhs)

}


func main() {

    IsLess[Int](Int(10), Int(20))

}

退出


Int does not satisfy Lesser: wrong method signature

    got  func (Int).Less(rhs Int) bool

    want func (Lesser).Less(rhs Lesser) bool

带有合同的原始草案将使这成为可能,但新草案却没有。


它也可以通过以下方式完成,但这会让您一遍又一遍地重复相同的约束,制动 DRY(并且 DRY 代码是泛型的目的)。如果所需的接口有多个方法,它也会使代码更加笨拙。


func IsLess[T interface { Less(T) bool }](lhs, rhs, T) bool {

    return lhs.Less(rhs)

}

有没有办法在新草案中使用预定义的接口来做到这一点?


婷婷同学_
浏览 172回答 1
1回答

杨__羊羊

定义接口类型Lesser和功能Isless如下:type Lesser[T any] interface {&nbsp; &nbsp; Less(T) bool}func IsLess[T Lesser[T]](lhs, rhs T) bool {&nbsp; &nbsp; return lhs.Less(rhs)}然后,以下代码可以顺利编译:type Apple intfunc (lhs Apple) Less(rhs Apple) bool {&nbsp; &nbsp; return lhs < rhs}type Orange intfunc (lhs Orange) Less(rhs Orange) bool {&nbsp; &nbsp; return lhs < rhs}func main() {&nbsp; &nbsp; fmt.Println(IsLess(Apple(10), Apple(20)))&nbsp; &nbsp;// true&nbsp; &nbsp; fmt.Println(IsLess(Orange(30), Orange(15))) // false&nbsp; &nbsp; // fmt.Println(IsLess(10, 30))&nbsp; &nbsp; // compilation error: int does not satisfy Lesser[T] (missing method Less)&nbsp; &nbsp; // fmt.Println(IsLess(Apple(20), Orange(30)))&nbsp; &nbsp; // compilation error: type Orange of Orange(30) does not match inferred type Apple for T}(游乐场)约束T Lesser[T]可以读作任何T有Less(T) bool方法的类型。我的两种自定义类型,Apple用它的Less(Apple) bool方法,和Orange用它的Less(Orange) bool方法,满足这个要求。作为信息,Java 泛型允许通过所谓的递归类型绑定来实现类似的技巧。有关此主题的更多信息,请参阅 Josh Bloch 的Effective Java第 3 版中的第 30 项(尤其是 p137-8)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go