> On Feb 26, 2017, at 8:26 AM, Derrick Ho via swift-evolution 
> <swift-evolution@swift.org> wrote:
> 
> In swift, a variadic argument can become an array without too much effort.
> 
> func foo(_ va: String...) {
>    let a: [String] = va
> }
> 
> However, it seems odd to me that an array can not be converted into a 
> variadic argument
> 
> foo(["a", "b", "c"]) // <-error
> foo("a", "b", "c") // no error
> 
> Other people have wondered about this too. 
> <http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift>
> 
> According to this thread <https://devforums.apple.com/message/970958#970958> 
> Doug Gregor says it is due to some type ambiguity. with Generics.
> 
> If type ambiguity is the issue, Do we have the option to cast it to the 
> correct type?
> 
> foo(["a", "b", "c"] as String...) // <- error.  doesn't consider String... to 
> be a type.

I think this needs to be done with a spread operator in order to disambiguate. 

foo(...["a", "b", "c”] 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
 
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator>

I like the idea. Its syntactic sugar so I am not sure how open the core team 
would be to adding it. 

> 
> What does the community think? Should we be granted some mechanism to turn an 
> array into a variadic argument?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to