这可以:
type constraint interface {
~float32 | ~float64
}
type foo[T constraint] struct {
val T
}
func (f *foo[float64]) setValToPi() {
f.val = 3.14
}
但是,如果我更改constraint为还包含int类型,则会遇到错误:
type constraint interface {
~float32 | ~float64 | ~int
}
type foo[T constraint] struct {
val T
}
func (f *foo[float64]) setValToPi() {
f.val = 3.14 // IncompatibleAssign: cannot use 3.14 (untyped float constant) as float64 value in assignment
}
为什么包含不属于同一“类型组”的类型的约束会导致此错误,我应该如何处理?
慕妹3242003
相关分类