登录
首页 >  Golang >  Go问答

泛型进阶:自引用接口限制

来源:stackoverflow

时间:2024-02-06 11:33:24 162浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《泛型进阶:自引用接口限制》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我有几个自定义类型需要以相同的方式处理。这似乎是泛型的完美用途。在此过程中,我需要在类型的实例上调用方法,这些方法返回相同类型的不同实例,然后我需要在这些返回的实例上调用方法,但我无法正常工作。为了解决这个问题,我制作了一组更简单的类型和一个过程来举例说明我遇到的问题。

这是一个没有泛型的工作示例,显示类型(circlesquare)和进程(.bigger().smaller()),稍后我将尝试抽象为泛型函数(在线演示) :

package main

import (
    "fmt"
)

type circle struct{ r float64 }

func newcircle(r float64) *circle  { return &circle{r: r} }
func (c *circle) radius() float64  { return c.r }
func (c *circle) bigger() *circle  { return &circle{r: c.r + 1} }
func (c *circle) smaller() *circle { return &circle{r: c.r - 1} }

type square struct{ s float64 }

func newsquare(s float64) *square   { return &square{s: s} }
func (s *square) side() float64     { return s.s }
func (s1 *square) bigger() *square  { return &square{s: s1.s + 1} }
func (s1 *square) smaller() *square { return &square{s: s1.s - 1} }

func main() {
    fmt.println(newcircle(3).bigger().smaller().radius()) // prints 3
    fmt.println(newsquare(6).bigger().smaller().side())   // prints 6
}

创建泛型函数时我要做的第一件事是定义类型约束:

type shapetype interface {
    *circle | *square
}

我将把 shapetype 传递给 process 方法,并且我需要能够调用 shapetype 实例上的方法,因此我需要定义另一个类型约束,它指定可以在 shapetype 上调用的方法:

type shape[st shapetype] interface {
    bigger() st
    smaller() st
}

有了这些,我就可以编写一个 process 方法(在线演示):

func process[st shapetype](s shape[st]) st {
    return s.bigger().smaller()
}

然而,这无法编译,因为 s.bigger() 的返回值是 st,而不是 shape[st],所以 go 不知道它可以在返回时调用 smaller() s.bigger() 的值。用 go 的话来说:

s.bigger().smaller 未定义(类型 st 没有字段或方法 smaller)

如果 bigger()smaller() 没有返回其接收器类型的实例,我可以写:

type shape interface {
    *circle | *square
    bigger()
    smaller()
}

func process[s shape](x s) s {
    x.bigger().smaller()
    return x // i guess we wouldn't even have to return x, but just for example's sake
}

相反,我需要写:

type shape interface {
    *circle | *square
    bigger() shape
    smaller() shape
}

看来 go 不喜欢自引用类型约束。

如果可以断言/转换具体类型到它符合的接口,那么我可以让它工作,但似乎不可能做到这一点(在线演示):

func process[ST ShapeType](s Shape[ST]) ST {
    s1 := s.Bigger()
    s2 := s1.(Shape[ST]) // go is not happy here
    return s2.Smaller()
}

为此,go 说:

无法对类型参数值 s1 使用类型断言(受 shapetype 约束的 st 类型变量)

我不知道还能尝试什么。

是否可以使用泛型来处理这些类型?如果是这样,怎么办?


正确答案


将您尝试的两个界面组合在一起:

type shape[st any] interface {
    *circle | *square
    bigger() st
    smaller() st
}

然后用类型参数本身实例化process的约束:

func process[st shape[st]](s st) st {
    return s.bigger().smaller()
}
  • 添加联合元素 *circle | *square 转换为 shape[st any] 意味着只有这两种类型才能实现该接口
  • 然后在方法签名中使用类型参数,例如 bigger() st,意味着无论传递哪种类型都有一个返回自身的方法。

如果您想将shapetype保留为单独的接口,则可以将shape编写为:

type shape[st any] interface {
    shapetype
    bigger() st
    smaller() st
}

您还可以使用 process 方法进行类型推断,没有任何问题:

func main() {
    c1 := NewCircle(3)
    c2 := process(c1) 
    fmt.Println(c2.Radius()) // prints 3 as expected
    fmt.Printf("%T\n", c2) // *main.Circle

    s1 := NewSquare(6)
    s2 := process(s1)
    fmt.Println(s2.Side()) // prints 6 as expected
    fmt.Printf("%T\n", s2) // *main.Square
}

最终演示:https://go.dev/play/p/_mR4wkxXupH

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>