[swift-evolution] Proposal: Remove implicit tuple splat behavior from function applications
swift at lng.la
swift at lng.la
Wed Jan 27 14:20:32 CST 2016
In the simple case that's a reasonable alternative, but in this case the loss is fairly significant. Tuple splatting allows the success and failure callbacks to be called generically, simplifying the implementation of fake API calls to a single line.
func getChatMessages(channel channel: ChatChannel, limit: Int, success: (chatMessages: [ChatMessage]) -> Void, failure: (error: NSError) -> Void) {
// Can't do this anymore:
// getChatMessagesEndpoint.handleCall(success, failure, callParams: (channel: channel, limit: limit))
getChatMessagesEndpoint.calls.append((channel: channel, limit: limit))
if getChatMessagesEndpoint.successStubs.count > 0 {
var stub = successStubs.removeFirst()
success(messages: stub.0, someOtherValue: stub.1)
}
else if getChatMessagesEndpoint.failureStubs.count > 0 {
var stub = failureStubs.removeFirst()
failure(error: stub.0, status: stub.1)
}
}
All that duplicated boilerplate really adds up once you have 10 or 20 different API calls.
In any case, this is a rare enough pattern (though I do think it's a very useful one) that it wouldn't be a huge loss for this feature to disappear for a while, but I do hope that it shows up again in another form at some point.
Jarod
> On Jan 27, 2016, at 11:41, Chris Lattner <clattner at apple.com> wrote:
>
>
>> On Jan 27, 2016, at 11:32 AM, Jarod Long <swift at lng.la <mailto:swift at lng.la>> wrote:
>>
>> But failure in this case is a function that takes two arguments, and failureStubs is an array of tuples that map to that function's arguments. How do the tuple values get mapped to the arguments if not via tuple splatting?
>
> Ah ok, I was looking at the success case I guess. You’re right. In this case, instead of :
>
> failure(failureStubs.removeFirst())
>
> you’d have to write something like this:
>
> let tmp = failureStubs.removeFirst()
> failure(tmp.0, tmp.1)
>
> -Chris
>
>>
>> Jarod
>>
>>> On Jan 27, 2016, at 11:28, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>>
>>>> On Jan 27, 2016, at 11:24 AM, swift at lng.la <mailto:swift at lng.la> wrote:
>>>>
>>>> Apologies -- I just realized I chose a bad example that isn't actually using the feature, but the usage would be here:
>>>>
>>>> if successStubs.count > 0 {
>>>> success(successStubs.removeFirst())
>>>> }
>>>> else if failureStubs.count > 0 {
>>>> failure(failureStubs.removeFirst())
>>>> }
>>>
>>> This isn’t using the feature either. You are passing a single value (returned by removeFirst) as a single argument. No spat is happening.
>>>
>>> -Chris
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160127/93e55b28/attachment.html>
More information about the swift-evolution
mailing list