[swift-evolution] [Idea] Passing an Array to Variadic Functions

Vladimir.S svabox at gmail.com
Wed Apr 20 01:02:15 CDT 2016


On 20.04.2016 2:07, Ricardo Parada wrote:
 > Would that require some compiler magic, i.e.
 >
 > foo(#splat(arr))

Probably yes. Although passing array to variadic func IMO is must-have 
feature, I don't think it will be used very often, so using of such 
compiler magic seems OK at the moment when we need this.

Also this solves the problem that passing array to variadic func is 
confusing about the arguments of function. If just pass array to such 
function - it is not clear what parameters of the function, is it variadic 
or "usual" function, is array just first argument or each value in array is 
argument.

The #splat solution is explicit, clearly mark function as variadic and 
array as a collection of parameters, it is *obvious* what is going there.

And as it is compiler business(at the compilation time) to interpret "arr" 
as a source of arguments for function - I think #splat is a correct solution.


To @Justin: I suggest to include this solution to the proposal.


>
>> On Apr 19, 2016, at 12:51 PM, Vladimir.S via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> I suggest to look at the subject of this thread and initial proposal:
>> "Swift should allow passing an *array to variadic functions*, or we should implement a type-safe `apply()` function in the standard library."
>>
>> I.e. the question is *if we want/need to be able to pass array to existed variadic function*. Not about removing the variadic feature("removing" is offtop for this poposal, as I understand)
>>
>> For example we have:
>> func foo(x: Int ...) {..}
>>
>> and later in code we have
>> var arr : [Int] = getSomehow()
>>
>> Should we be able to call foo in this way:
>>
>> foo(arr)
>>
>> Or how we can call foo(..) about array of values, not values as list of parameters.
>>
>>
>>> On 19.04.2016 19:34, Jeremy Pereira via swift-evolution wrote:
>>>
>>>> On 19 Apr 2016, at 07:51, Haravikk via swift-evolution <swift-evolution at swift.org> wrote:
>>>>
>>>>
>>>>
>>>> I think the question really is whether not having to add square brackets is really enough to justify a whole language feature? Basically you’ve got two ways you could do this:
>>>
>>> No, that’s not the question. The question is whether the downside to variadic parameters is really enough to justify _removing_ an existing language feature.
>>>
>>> The burden of justification should be on those people wanting to change the language, not on those wanting to maintain the status quo and “I don’t like it” or “I think it makes code a tiny bit less readable” is not sufficient justification, in my opinion because you already have the option not to use the feature. Whereas, if you remove the feature, those people of the opinion “it makes code a tiny bit more readable” have nowhere to go.
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list