Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mixing "exploded" slices and regular parameters in variadic functions

Tags:

I'm wondering why it's not possible to do the following in go:

func main() {     stuff := []string{"baz", "bla"}     foo("bar", stuff...) }  func foo(s ...string) {     fmt.Println(s) } 

In my understanding, slice... "explodes" the slice so it can be used for multi argument function calls. So the above example should actually expand to foo("bar", "baz", "bla").

foo(stuff...) works as expected, no surprises here, but in the example above, the compiler complains about too many arguments.

Is this a desired limitation? I'm coming from a ruby background where a foo("bar", *stuff) is perfectly fine (and is, at least in my book, the same thing), that's why this surprises me.

like image 977
Pascal Avatar asked Feb 20 '15 09:02

Pascal


People also ask

What symbols are used in a function declaration to indicate that it is a variadic function?

A function with a parameter that is preceded with a set of ellipses ( ... ) is considered a variadic function. The ellipsis means that the parameter provided can be zero, one, or more values.

How do you use Variadic arguments?

In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis(…) as the last parameter. This enables access to variadic function arguments.

Why do we use variadic parameters in Swift?

In Swift, variadic parameters are the special type of parameters available in the function. It is used to accept zero or more values of the same type in the function. It is also used when the input value of the parameter is varied at the time when the function is called.

What is variadic function in Golang?

A variadic function is a function that accepts a variable number of arguments. In Golang, it is possible to pass a varying number of arguments of the same type as referenced in the function signature.


1 Answers

The value for a variadic argument can be specified either by enumerating the elements, or using an existing slice, specified by its name followed by ....

You want to mix the 2 possible ways which is not permitted by the Go Language Specification (Passing arguments to ... parameters).

If the first form is used (enumerating the elements):

The value passed [as the variadic parameter] is a new slice of type []T with a new underlying array whose successive elements are the actual arguments.

If the latter is used (passing an existing slice followed by ...) no new slice is created, the one you pass is used as is. And the passed slice can only be used to specify the value of one – the final – variadic parameter. Attempting to pass both a single element and a slice will not match the signature (the parameter list in this case) of your function and you'll get an error:

too many arguments in call to foo 

There is no actual "exploding" involved in Go, the term is just used in other languages to help visualize that the passed array or slice will not be an element of the variadic parameter but will be the value of variadic parameter itself.

Mixing the 2 would require to allocate a new slice because obviously the existing slice cannot be used.

like image 71
icza Avatar answered Sep 28 '22 23:09

icza