[swift-evolution] [Proposal] Allow passing a variadic parameter to another variadic function

Christopher Boyd xpboyd at gmail.com
Fri May 20 11:08:37 CDT 2016


Ricardo,

That'd work, but I was thinking more along the lines of:
- func add(numbers: Double...) -> Double {
- var total: Double = 0
- for number in numbers {
- total += number
- }
- return total
- }
- func arithmeticMean(numbers: Double...) -> Double {
- return add(numbers) / Double(numbers.count)
- }

Certainly, #splat would work, but it may be slightly more confusing to
someone that hasn't seen the splat operator before:
- func arithmeticMean(numbers: Double...) -> Double {
- return add(#splat(numbers)) / Double(numbers.count)
- }

On Fri, May 20, 2016 at 11:52 AM Ricardo Parada <rparada at mac.com> wrote:

>
> Hi Christopher,
>
> Do you think something like this would work if there was a #splat() magic
> operator like this:
>
> let values = [1, 2, 3, 4, 5]
> let mean = arithmeticMean(#splat(values))
>
>
>
> On May 20, 2016, at 11:19 AM, Christopher Boyd via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Currently, Swift doesn't have a way to easily pass a variadic parameter to
> another variadic function because the variadic parameter itself has already
> been collected into an array.
>
> There was a prior posting a few weeks back about allowing arrays to be
> passed to a variadic parameter which would resolve this very specific issue:
>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/015185.html
>
> Unfortunately, that thread devolved into an argument on whether or not
> variadic parameters should be removed entirely.
>
> I, certainly, see merit in having functions that can accept a variable
> number of arguments and variadic functions should seem intuitive to anyone
> else that might be reading the code.
>
> Swift's documentation presents a very intuitive example:
>
>    1. arithmeticMean(1, 2, 3, 4, 5)
>
> It's obvious what this function is doing, and any intelligent person would
> expect 3 as the result.  Even WolframAlpha knows how to interpret this:
> http://www.wolframalpha.com/input/?i=arithmeticMean(1,+2,+3,+4,+5)
>
> Moreover, a function can only have ONE variadic parameter, which I think
> reduces any possible confusion.
>
> Regardless, if variadic parameters are going to stay in Swift, then I
> think they should at least support chaining to another function.
>
> The most obvious way of allowing this would be to allow variadic
> parameters to accept an array.
>
> However, I think that if an array (of the parameter type) is specified for
> a variadic parameter, then no other values should be allowed.  This
> eliminates any unnecessary complexity of potentially having to add values
> to the array.
>
> - Christopher Boyd
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160520/12201556/attachment.html>


More information about the swift-evolution mailing list