What is the reason that, given a (variadic) function
func varargs(n ...int) {}
it can be called like
varargs(1, 2, 3, 4) // Fixed number of arguments
but not with an array:
a := [4]int{1, 2, 3, 4} // Fixed number of elements
varargs(a...) // Error: cannot use (type [4]int) as type []int in argument
I understand why
var s []int = a
wouldn't work: it prevents accidental misuse, requiring manual slicing:
s := a[:]
But why does this restriction extend to calls to variadic functions?
Bonus question:
Conversely, why would calling
func fourargs(w, x, y, z int) {}
with a 4-element array like
fourargs(a...) // Error: not enough arguments in call have ([4]int...)
// want (int, int, int, int)
also be forbidden?
It can be type-checked at compile time.