Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is forwarding variadic parameters invalid?

Consider the variadic function parameter:

func foo(bar:Int...) -> () { }

Here foo can accept multiple arguments, eg foo(5,4). I am curious about the type of Int... and its supported operations. For example, why is this invalid?

func foo2(bar2:Int...) -> () {
    foo(bar2);
}

Gives a error:

Could not find an overload for '_conversion' that accepts the supplied arguments

Why is forwarding variadic parameters invalid?

What is the "conversion" the compiler is complaining about?

like image 200
yizzlez Avatar asked Jun 04 '14 21:06

yizzlez


1 Answers

When you call foo, the compiler expects a series of arguments, each of which must be an Int.

In the body of foo2, bar2 summarises all the passed arguments and actually has the type Int[] for all practical purposes. Thus, you cannot pass it directly to foo — as foo wants Int arguments, and not an Int[].

As for a solution to this: see my answer to this question.

like image 179
Jean-Philippe Pellet Avatar answered Nov 11 '22 20:11

Jean-Philippe Pellet