[swift-evolution] [Proposal] Variadics as Attribute
me at lmpessoa.com
Fri Jul 8 06:03:14 CDT 2016
It is definitely not hard to solve any issues here (if you consider only
the basic variadics). Int... is nothing more than [Int], so essentially
func doSomething(args : Int...)
is also just
func doSomething(args : [Int])
We just have to think of it like some form of function overloading and the
compiler can automatically handle that.
As for enabling usage of other types as the result of variadics, I don't
see how that can be a benefit (unless you're just trying to write little
bit less on your own code). You would have to add some extra code on the
compiler to check whether you can use that type for your variadics argument
and may incur in more changes to enable handling different classes
possible. I would also expect to be able to use dictionaries as variadics
with this syntax, and that would be confusing too.
Should this pass like it is, it would not surprise me if my apps take more
time to compile. Now, if the problem is just being able to pass an array to
a variadics argument, my solution is cleaner, incur in less changes to the
language and is backwards compatible.
On Friday, 8 July 2016, Haravikk via swift-evolution <
swift-evolution at swift.org> wrote:
> On 8 Jul 2016, at 10:31, Pyry Jahkola <pyry.jahkola at iki.fi
> If you take this function for example,
> func printThem(@variadic _ values: [Any])
> then what would `values` be in the following calls, and why?
> printThem() // clearly 
> printThem(1) // clearly 
> printThem(1, 2) // clearly [1, 2]
> printThem(, ) // clearly [, ]
> printThem() //  or [], which one?
> printThem() //  or []?
> printThem([1, 2]) // [1, 2] or [[1, 2]]?
> I think it would be less painful a change (i.e. can be delayed past Swift
> 3) if we just augment what we have (the `...` argument declaration syntax)
> with a way to expand an Array in place of a variadic argument:
> func printThem(_ values: Any...)
> printThem(1) // values == 
> printThem() // values == []
> // Possible expansion postfix syntax:
> printThem(...) // values == 
> // Expanding a non-array sequence:
> let things: Set<Int> = [1, 2, 3]
> Good point, but actually these don't seem mutually exclusive; the three
> problem examples you gave could be resolved either by requiring the extra
> set of square brackets for clarity, and/or some kind of expansion like you
> suggest, either an operator or even possibly repurposing the attribute at
> the call-site for consistency. For example:
> printThem() // warning, ambiguous
> printThem([])// non-variadic
> printThem(@variadic ) // absolutely definitely 100% a variadic call
> printThem(,) // alternative to an operator/attribute for
> variadic disambiguation?
> On 8 Jul 2016, at 10:42, Tino Heth <2th at gmx.de
> I'm not sure if the timing of this proposal is coincidence (I recently
> revived a discussion to remove variadics)
> It is definitely not a coincidence ;)
> I participated in the discussion on removal in the past and actually
> posted a preliminary version of this there, so the recent posts reminded me
> to put it into a proper proposal. Personally like some I'd just remove the
> feature completely, but it seems enough people want to keep it that it's
> worth exploring alternatives as there seems a fairly even mix of those for
> and against the feature.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution