登录
首页 >  Golang >  Go问答

解析Go语言中新出现的波形符~的含义是什么?

来源:stackoverflow

时间:2024-02-14 13:48:25 208浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《解析Go语言中新出现的波形符~的含义是什么?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

go 引入了新代币 ~

~t 表示具有基础类型 t 的所有类型的集合

但是我看不懂,请高人帮忙解释一下。

以下是一个示例。

type Ordered interface {
      Integer | Float | ~string
}

正确答案


在 go 泛型中,~ 波形符标记以 ~t 的形式使用,表示基础类型为 t 的类型集。

它在 generics proposal 中也被称为“近似”约束元素,用简单的语言解释了它的好处:

列出单一类型本身是没有用的。为了满足约束条件,我们不仅希望能够说 int,还希望能够说“基础类型为 int 的任何类型”。 [...] 如果程序使用 type mystring string,则该程序可以使用 < 运算符以及 mystring 类型的值。应该可以使用 mystring 类型实例化[函数]。

如果您想要正式参考,语言规范已将底层类型的定义放在 its own section 中:

每个类型 t 都有一个基础类型:如果 t 是预先声明的布尔、数字或字符串类型之一,或者类型文字,则相应的基础类型是 t 本身。否则,t 的基础类型是 t 在其类型声明中引用的类型的基础类型。

这涵盖了类型文字和其他具有绑定标识符的复合类型的常见情况,或者您通过预先声明的标识符定义的类型,这是泛型提案中提到的情况:

// underlying type = struct literal -> itself -> struct { n int }
type foo struct {
    n int
}

// underlying type = slice literal -> itself -> []byte
type byteslice []byte

// underlying type = predeclared -> itself -> int8
type myint8 int8

// underlying type = predeclared -> itself -> string
type mystring string

实际含义是,类型集仅包含精确元素的接口约束不允许您自己定义的类型:

// hypothetical constraint without approximation elements
type exactsigned interface {
    int | int8 | int16 | int32 | int64
}

// cannot instantiate with myint8
func echoexact[t exactsigned](t t) t { return t }

// constraints.signed uses approximation elements e.g. ~int8
// can instantiate with myint8
func echo[t constraints.signed](t t) t { return t }

与其他约束元素一样,您可以在联合中使用近似元素,如 constraints.Signed 中那样,或者在带或不带语法糖的匿名约束中使用。值得注意的是,只有一个 approx 元素的语法糖是有效的:

// anonymous constraint
func echofixedsize[t interface { ~int8 | ~int32 | ~int64 }](t t) t { 
    return t 
}

// anonymous constraint with syntactic sugar
func echofixedsizesugar[t ~int8 | ~int32 | ~int64](t t) t { 
    return t 
}

// anonymous constraint with syntactic sugar and one element
func echofixedsizesugarone[t ~int8](t t) t { 
    return t 
}

正如上面所预期的,近似元素的常见用例是需要具有方法的复合类型(切片、结构体等)。在这种情况下,您必须绑定标识符:

// must bind identifier in order to declare methods
type byteseq []byte

func (b byteseq) dosomething() {}

现在近似元素可以方便地使用 byteseq 进行实例化:

// byteseq not allowed, or must convert func argument first
func foobar[t interface { []byte }](t t) { /* ... */ }


// byteseq allowed
func bazquux[t interface { ~[]byte }](t t) { /* ... */ }

func main() {
    b := []byte{0x00, 0x01}
    seq := byteseq{0x02, 0x03}

    foobar(b)           // ok
    foobar(seq)         // compiler error
    foobar([]byte(seq)) // ok, allows inference
    foobar[[]byte](seq) // ok, explicit instantiation, then can assign seq to argument type []byte

    bazquux(b)          // ok
    bazquux(seq)        // ok
}

注意:您不能将近似标记与类型参数一起使用:

// invalid!
type anyapprox[t any] interface {
    ~t
}

不仅有新的标记,还有接口的新语法。除了方法约束之外,您还可以声明带有类型约束的接口。

要满足接口,类型必须同时满足方法约束和类型约束。

来自docs

对于具有 int 的“基础类型”的类型,这意味着该类型采用以下形式:

type sometype int

并且为了满足方法约束,必须声明具有指定签名的方法:

func (v SomeType) String() string {
  return fmt.Sprintf("%d", v)
}

终于介绍完啦!小伙伴们,这篇关于《解析Go语言中新出现的波形符~的含义是什么?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

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