type Number interface {
int | int64 | float64
}
type NNumber interface {
}
//interface contains type constraints
//type NumberSlice []Number
type NNumberSlice []NNumber
func main() {
var b interface{}
b = interface{}(1)
fmt.Println(b)
// interface contains type constraints
// cannot use interface Number in conversion (contains specific type constraints or is comparable)
//a := []Number{Number(1), Number(2), Number(3), Number(4)}
//fmt.Println(a)
aa := []interface{}{interface{}(1), interface{}(2), interface{}(3), 4}
fmt.Println(aa)
aaa := []NNumber{NNumber(1), NNumber(2), NNumber(3), 4}
fmt.Println(aaa)
}
why the Number
slice a
couldn't be initialized like that?
NumberSlice
and NNumberSlice
look like similarly, but what mean type constraints, it looks strange grammar
CodePudding user response:
The error message is clear:
interface contains type constraints
cannot use interface Number in conversion (contains specific type constraints or is comparable)
And the language specifications explicitly disallow using interfaces with type elements — or that embed comparable
— as anything other than type parameter constraints (the quote is buried in the paragraph Interface types):
Interfaces that contain non-interface types, terms of the form ~T, or unions may only be used as type constraints, or as elements of other interfaces used as constraints. They cannot be the types of values or variables, or components of other, non-interface types.
Your Number
interface contains a non-interface type element (a union is considered as one type element, made up of several type terms:
type Number interface {
int | int64 | float64
}
In the initialization of the variable a
, you are attempting to use Number
in a type conversion Number(1)
, and this is not allowed.
You can only use Number
as a type parameter constraint, i.e. to restrict the types allowed for instantiation of a generic type or function. For example:
type Coordinates[T Number] struct {
x, y T
}
func sum[T Number](a, b T) T {
return a b
}