[swift-evolution] Proposal: Remove implicit tuple splat behavior from function applications

swift at lng.la swift at lng.la
Wed Jan 27 13:03:21 CST 2016


I'm +1 if this will eventually be replaced with a better-designed version, but I do use it and would be sad to see it go. I've found it very helpful for testing:

protocol APIType {
    func getChatMessages(channel channel: ChatChannel, limit: Int, success: (chatMessages: [ChatMessage]) -> Void, failure: (error: NSError) -> Void)
}

class FakeAPI: APIType {
    var getChatMessagesEndpoint = FakeAPIEndpoint(
        calls:        [(channel: ChatChannel, limit: Int)](),
        successStubs: [[ChatMessage]](),
        failureStubs: [NSError]()
    )
    
    func getChatMessages(channel channel: ChatChannel, limit: Int, success: (chatMessages: [ChatMessage]) -> Void, failure: (error: NSError) -> Void) {
        getChatMessagesEndpoint.handleCall(success, failure, callParams: (channel: channel, limit: limit))
    }
}

struct FakeAPIEndpoint<CallParams, SuccessParams, FailureParams> {
    private(set) var calls:        [CallParams]
    private(set) var successStubs: [SuccessParams]
    private(set) var failureStubs: [FailureParams]
    
    mutating func stubSuccess(params: SuccessParams) {
        successStubs.append(params)
    }
    
    mutating func stubFailure(params: FailureParams) {
        failureStubs.append(params)
    }
    
    private mutating func handleCall(success: (SuccessParams) -> Void, _ failure: (FailureParams) -> Void, call: CallParams) {
        calls.append(call)
        
        if successStubs.count > 0 {
            success(successStubs.removeFirst())
        }
        else if failureStubs.count > 0 {
            failure(failureStubs.removeFirst())
        }
    }
}

It's really nice to have that handleCall method that can call the success or failure callback automatically rather than reimplementing that functionality in every fake API call.

Jarod

> On Jan 27, 2016, at 10:16, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Jan 27, 2016, at 2:18 AM, Nisse Bergman <nisse at potmo.com> wrote:
>> 
>> -1 I use this in both my mocking API and in JSON deserialising/serializing.
>> I think this is a great thing to have.
> 
> Ok, I’m definitely interested in knowing more.  Please include a code sample, showing both the declaration being splatted into and the call sites.  Otherwise, I can’t tell how much value this feature is adding, and what the pain would be if it were removed.  Thanks!
> 
> -Chris
> 
>> 
>>> On 27 Jan 2016, at 11:07, David Waite via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> I used this just last week! But only as an illustration
>>> 
>>> +1
>>> 
>>> One comment though, a splat operator need not be purely syntactic sugar - it could be the way arrays are applied to variadic functions.
>>> 
>>> -DW
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>> 
> 
> _______________________________________________
> 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/20160127/8e0d092a/attachment.html>


More information about the swift-evolution mailing list