[swift-evolution] named parameters
Chris Lattner
clattner at apple.com
Fri Jan 22 18:27:12 CST 2016
On Jan 22, 2016, at 3:10 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
>>
>>>> Additionally, there is a hidden(?) feature in Swift that's imho quite cool:
>>>> The ability to call a function with a tuple containing the arguments
>>>> let args = (4.0, exponent: 4.0)
>>>> printPowWithBase(args)
>>>>
>>>> In this case, you end up with a first parameter without any indication of its role.
>>>
>>> This feature is likely to get redesigned soon. (It has other problems.)
>> Can you elaborate? I did not encounter many usecases yet, but I think it could be handy to configure a bunch of similar objects — and if it would be possible to capture the parameter tuple inside the called function, this could be forwarded to a function with the same signature easily… (especially, but not only the implementation of super)
>
> In some contexts it's hard to distinguish between "the first parameter" and "the tuple of all parameters", and we have some weird inconsistencies where "foo(x)" and "foo((x))" do the same thing sometimes and different things other times.
Here’s a simple example to show the ambiguity:
func foo(a : Any…) {}
let x = (1,2,3)
foo(x) // 1 parameter or 3?
> There's more complexity here but I don't have it paged in; it's not so relevant to this discussion
Incidentally, IMO the likely solution is for this to just make this explicit somehow. * isn’t really right right sigil for this, but you could imagine something like this:
printPowWithBase(*args)
Where the * (or whatever symbol actually makes sense) is a “flatten” or “expand inline” operation for the tuple argument. This makes it unambiguous whether splatting is happening or not.
func foo(a : Any…) {}
let x = (1,2,3)
foo(x) // 1 parameter
foo(*x) // 3 parameters
Alternatively, the argument could be made that we should just remove this splatting behavior. I haven’t heard of any super-compelling use-cases for it. Adding a sigil for this operation would expose the complexity that the behavior underlines.
-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160122/e8b1a50e/attachment.html>
More information about the swift-evolution
mailing list