<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Feb 26, 2017, at 8:26 AM, Derrick Ho via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">In swift, a variadic argument can become an array without too much effort.<div class=""><br class=""></div><div class="">func foo(_ va: String...) {</div><div class=""> let a: [String] = va</div><div class="">}</div><div class=""><br class=""></div><div class="">However, it seems odd to me that an array can not be converted into a variadic argument</div><div class=""><br class=""></div><div class="">foo(["a", "b", "c"]) // <-error</div><div class="">foo("a", "b", "c") // no error<br class=""></div><div class=""><br class=""></div><div class=""><a href="http://stackoverflow.com/questions/24024376/passing-an-array-to-a-function-with-variable-number-of-args-in-swift" class="">Other people have wondered about this too.</a><br class=""></div><div class=""><br class=""></div><div class="">According to <a href="https://devforums.apple.com/message/970958#970958" class="">this thread</a> Doug Gregor says it is due to some type ambiguity. with Generics.</div><div class=""><br class=""></div><div class="">If type ambiguity is the issue, Do we have the option to cast it to the correct type?</div><div class=""><br class=""></div><div class="">foo(["a", "b", "c"] as String...) // <- error. doesn't consider String... to be a type.<br class=""></div></div></div></blockquote><div><br class=""></div><div>I think this needs to be done with a spread operator in order to disambiguate. </div><div><br class=""></div><div>foo(...["a", "b", "c”] </div><div><br class=""></div><div><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator" class="">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator</a></div><div><br class=""></div><div>I like the idea. Its syntactic sugar so I am not sure how open the core team would be to adding it. </div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">What does the community think? Should we be granted some mechanism to turn an array into a variadic argument?</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>