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

Haravikk swift-evolution at haravikk.me
Tue Apr 19 01:51:32 CDT 2016

> On 19 Apr 2016, at 00:14, Justin Jia via swift-evolution <swift-evolution at swift.org> wrote:
> variadic functions are really easy to understand. 
> For example:
> sum(x)
> average(x)
> NSLog("%d", x)
> NSString.stringWithFormat("%d", x)

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:

	NSString.stringWithFormat(“%d”, x)
	NSString.stringWithFormat(“%d”, [x])

The second format is a tiny bit longer, but more explicit at the call-site about what is going on, whereas the first, if you don’t know the function, is indistinguishable from a function that takes a single argument of whatever type x is.

I’m kind of in the remove variadic functions camp as a result, as I’d prefer to be more explicit about the type rather than less, and it’s not as if two square brackets is going to kill anyone. The main issue for me is whether there are ways for variadic functions to be optimised that aren’t available to a full-blown array? I don’t know enough about the compiler side to comment on that, but it seems like maybe there could be an optimisation for variadic argument lists.

The other question is how would we implement ArrayLiteralConvertible (and DictionaryLiteralConvertible) without variadic functions? As if the aim is to instantiate an array alternative without having to have a full array instance first then variadic functions may be able to allow this? Again I’m not certain on that point.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160419/7467870f/attachment.html>

More information about the swift-evolution mailing list