[swift-evolution] array splatting for variadic parameters
Slava Pestov
spestov at apple.com
Thu Feb 9 20:07:13 CST 2017
Hi Dimitri,
I think this is a desirable feature, but nobody has got around to designing and implementing it yet.
Another case where it comes up is initializer inheritance. Suppose I have:
class Base {
init(x: Int…) {}
}
class Derived : Base {
}
Currently, Derived does not inherit Base.init(), because we have no way to synthesize the AST which performs this ‘forwarding’.
In fact, if somebody wanted to attempt an implementation of this, handling this case first would be easier because then you can implement the AST node for splatting a vararg and get it working without worrying about parser changes (probably you can shoehorn it into TupleShuffleExpr). Then after a design for the syntax has been accepted, add the parser support on top.
Slava
> On Feb 9, 2017, at 3:51 PM, Dimitri Racordon via swift-evolution <swift-evolution at swift.org> wrote:
>
> Hello everyone.
>
> I understand that topic has already been discussed in the past, but I failed to get any information on its current state of affairs.
>
> I guess the subject of this mail is explicit, but to make sure I’m clear, here's a small snippet that illustrates the problem:
>
> func f(args: Int…) {
> // Some implementation ...
> }
> // Now it's impossible to call f without explicitly naming its parameters.
>
> For many use-cases, this problem can be solved by overloading f so that it explicitly accepts an array.
>
> func f(_ args: [Int]) {
> // Some implementation ...
> }
>
> func f(_ args: Int…) {
> f(args)
> }
>
> Some people also advocate (myself generally included) that one should prefer the signature explicitly marking args as an array, as the syntactic overhead of wrapping the arguments with “[]” when calling f is arguably bearable. However, in some other situations, this approach might not be applicable. For instance, one may simply not be able to modify the original function. Another use-case may be a function that should forward its own variadic parameters.
>
> In a variety of other languages, there exists a way to do this. For instance, Python can “unpack” (or splat) a list into function arguments by prefixing it with *:
>
> def f(*args):
> # Some implementation …
>
> f(*[1, 2, 3]) # == f(1, 2, 3)
>
> I was wondering if such feature could be supported by Swift, and if not, why.
>
> Syntactically, I like the use of “…”, which would mirror function signatures:
>
> f(…[1, 2, 3]) // == f(1, 2, 3)
>
> Thanks.
>
> --
> Dimitri Racordon
>
> _______________________________________________
> 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/20170209/44a4a587/attachment.html>
More information about the swift-evolution
mailing list