如何将 `type Int32 int32` 转换为 `int32`?

huangapple go评论109阅读模式
英文:

How to convert `type Int32 int32` to `int32`?

问题

我们有以下生成的代码(所以我们不能将其更改为type Enum interface { int32 }):

type Enum int32

有很多这样的类型定义为int32

我试图创建一个通用函数,可以将这些类型定义的切片转换为[]int32

func ConvertToInt32Slice[T int32](ts []T) []int32 {
	return lo.Map(ts, func(t T, _ int) int32 {
		return int32(t)
	})
}

但是以下代码会报错:Type does not implement constraint 'interface{ int32 }' because type is not included in type set ('int32')

type Int32 int32
var int32s []Int32
i32s := ConvertToInt32Slice(int32s)

需要如何实现这个通用函数,以便按预期使用它?

英文:

We have the following code that's generated (so we can't change it to type Enum interface { int32 }):

type Enum int32

There are a bunch of these type definitions to int32.

I'm trying to create a generic function that can convert a slice of such type definitions to a []int32:

func ConvertToInt32Slice[T int32](ts []T) []int32 {
	return lo.Map(ts, func(t T, _ int) int32 {
		return int32(t)
	})
}

but the following complains with Type does not implement constraint 'interface{ int32 }' because type is not included in type set ('int32'):

type Int32 int32
var int32s []Int32
i32s := ConvertToInt32Slice(int32s)

What's needed to implement the generic function so it can be used as intended?

答案1

得分: 3

看起来你想要一个通用函数,可以操作所有具有“基础类型”为int32的类型。这就是波浪符(~)的作用。你应该改变函数的签名为:

func ConvertToInt32Slice[T ~int32](ts []T) []int32

参考链接:https://stackoverflow.com/questions/70888240/whats-the-meaning-of-the-new-tilde-token-in-go

constraints.Integer也可以使用,因为它的定义如下:

type Integer interface {
    Signed | Unsigned
}

https://pkg.go.dev/golang.org/x/exp/constraints#Integer

其中定义了:

type Signed interface {
    ~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
    ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

这样就可以得到所有基础类型为整数类型的类型。

如果你只对基础类型为int32的类型感兴趣,你应该使用~int32

英文:

It looks like you want a generic function that operates on all types that have the underlying type int32. That's what the tilde (~) token is for.
You should change the signature of your function to:

func ConvertToInt32Slice[T ~int32](ts []T) []int32

See also: https://stackoverflow.com/questions/70888240/whats-the-meaning-of-the-new-tilde-token-in-go

constraints.Integer also works, because that is defined as:

type Integer interface {
	Signed | Unsigned
}

https://pkg.go.dev/golang.org/x/exp/constraints#Integer

which is defined as:

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

So that gives you all types that have any of the integer types as the underlying types.

If you're only interested in types that have int32 as their underlying type, you should use ~int32.

huangapple
  • 本文由 发表于 2022年11月26日 10:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/74579223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定