在 Go 泛型中,为什么我不能对顺序运算符使用可比约束?
In Go generics, why can't I use comparable constraint with order operators?
我正在探索 go 泛型(1.18 测试版),并且有与比较两个数值相关的问题(一种采用两个值和 returns 更大数字的简单方法)。
为此,我正在创建一个涵盖此类型集的自定义数字类型(在函数 getBiggerNumber
中):
int | int8 | int16 | int32 | int64 | float32 | float64
但是,又添加了一个函数,但这次没有使用自定义类型,而是使用了 comparable
内置约束(在函数 getBiggerNumberWithComparable
中)。
但是下面的代码给出了这个方法的错误,因为
"invalid operation: cannot compare t1 > t2 (operator > not defined on T)"?
知道为什么 >
操作对内置的可比较类型不起作用吗?
package main
import "fmt"
type numbers interface {
int | int8 | int16 | int32 | int64 | float32 | float64
}
func getBiggerNumber[T numbers](t1, t2 T) T {
if t1 > t2 {
return t1
}
return t2
}
func getBiggerNumberWithComparable[T comparable](t1, t2 T) T {
if t1 > t2 { // ./generics-sample.go:17:5: invalid operation: cannot compare t1 > t2 (operator > not defined on T)
return t1
}
return t2
}
func main() {
fmt.Println(getBiggerNumber(2.5, -4.0))
fmt.Println(getBiggerNumberWithComparable(2.5, -4.0))
}
comparable
是支持相等运算符 ==
和 !=
的类型的约束。语言规范在 Type constraints.
中对此进行了定义
值得注意的是,这包括任何可用作映射键的内容,包括数组、具有可比字段的结构 。
的确,在Go语言规范中,comparison operators包含顺序运算符为(<
, >
, <=
, >=
) .这种术语选择可能会让您感到困惑。然而,规范也消除了歧义:
The equality operators ==
and !=
apply to operands that are comparable. The ordering operators <
, <=
, >
, and >=
apply to operands that are ordered.
在 Go 1.18 中,支持 >
和 <
等顺序运算符的可用约束是 constraints.Ordered
1:
type Ordered interface {
Integer | Float | ~string
}
1:请注意包 golang.org/x/exp
是实验性的。它的内容不保证与新的 Go 版本向后兼容,但你总是可以将你需要的定义复制粘贴到你自己的代码中
我正在探索 go 泛型(1.18 测试版),并且有与比较两个数值相关的问题(一种采用两个值和 returns 更大数字的简单方法)。
为此,我正在创建一个涵盖此类型集的自定义数字类型(在函数 getBiggerNumber
中):
int | int8 | int16 | int32 | int64 | float32 | float64
但是,又添加了一个函数,但这次没有使用自定义类型,而是使用了 comparable
内置约束(在函数 getBiggerNumberWithComparable
中)。
但是下面的代码给出了这个方法的错误,因为
"invalid operation: cannot compare t1 > t2 (operator > not defined on T)"?
知道为什么 >
操作对内置的可比较类型不起作用吗?
package main
import "fmt"
type numbers interface {
int | int8 | int16 | int32 | int64 | float32 | float64
}
func getBiggerNumber[T numbers](t1, t2 T) T {
if t1 > t2 {
return t1
}
return t2
}
func getBiggerNumberWithComparable[T comparable](t1, t2 T) T {
if t1 > t2 { // ./generics-sample.go:17:5: invalid operation: cannot compare t1 > t2 (operator > not defined on T)
return t1
}
return t2
}
func main() {
fmt.Println(getBiggerNumber(2.5, -4.0))
fmt.Println(getBiggerNumberWithComparable(2.5, -4.0))
}
comparable
是支持相等运算符 ==
和 !=
的类型的约束。语言规范在 Type constraints.
值得注意的是,这包括任何可用作映射键的内容,包括数组、具有可比字段的结构
的确,在Go语言规范中,comparison operators包含顺序运算符为(<
, >
, <=
, >=
) .这种术语选择可能会让您感到困惑。然而,规范也消除了歧义:
The equality operators
==
and!=
apply to operands that are comparable. The ordering operators<
,<=
,>
, and>=
apply to operands that are ordered.
在 Go 1.18 中,支持 >
和 <
等顺序运算符的可用约束是 constraints.Ordered
1:
type Ordered interface {
Integer | Float | ~string
}
1:请注意包 golang.org/x/exp
是实验性的。它的内容不保证与新的 Go 版本向后兼容,但你总是可以将你需要的定义复制粘贴到你自己的代码中