[swift-evolution] [pitch] Variadic Arguments should accept Arrays
Jose Cheyo Jimenez
cheyo at masters3d.com
Mon Feb 27 02:04:07 CST 2017
> On Feb 26, 2017, at 7:00 PM, Derrick Ho <wh1pch81n at gmail.com> wrote:
>
> Well, I have found these discussions...
>
> It was marked as a bug and then directed to swift evolution.
>
> @jose, the prefix operator you mention looks good, but in the past discussions they wanted a postfix operator so that it would match the declaration. I think I like the postfix operator.
>
> func foo(_ a: String...) {}
>
> foo(["a", "b", "c"]...) //<- This looks clear. It is turning the array into a variadic argument.
This is problematic because of the similarities with the proposed one sided ranges in the new String manifesto. These operations produce slices which are collection.
c[i...]... // first dots one sided range, second the spread operator.
a prefix ... operator would be a little better here but just barely.
...c[i...]
I think a prefix of * or ... would work great for declaring and deconstructing.
func foo(_ a: ...[Int]) {} // declared as array required.
foo(...[1,2,3,4])
foo(...(0,1,2,3))
func foo(_ a: *[Int]) {}
foo(*[1,2,3,4])
foo(*(0,1,2,3))
foo(1,2,3,4)
>
> However, that thread was closed because it wasn't "discussed" enough prior making the pull request.
>
> therefore, if people want it, they got to cast their vote in here by either supporting it or opposing it.
>
> I like my original suggestion since it is more explicit.
>
> foo(["a", "b", "c"] as String...)
>
>
>
> On Sun, Feb 26, 2017 at 7:59 PM Jose Cheyo Jimenez <cheyo at masters3d.com> wrote:
>>> On Feb 26, 2017, at 8:26 AM, Derrick Ho via swift-evolution <swift-evolution at 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.
>>>
>>> According to this thread 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
>>
>> 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 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/20170227/a8b9dcfe/attachment.html>
More information about the swift-evolution
mailing list