Inconsistent behavior for tuple used in a returned function vs regular function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Inconsistent behavior for tuple used in a returned function vs regular function

Boon Chew


In the following two examples the function returns the same type even though one takes a tuple while the other takes two arguments. From the caller standpoint, there is no difference in the way you call it regardless of whether the function takes a tuple or regular arguments.

When the same code is used on two regular functions with identical signatures as the returned functions, the same syntax no longer works for the tuple version.  Why the inconsistencies?

e.g.

func testFunctionUsingTuple()->(Int, String)->Void {
    func t(x:(Int, String)) {
        print("\(x.0) \(x.1)")
    }

    return t
}

func testFuncUsingArgs()->(Int, String)->Void {
    func t(x:Int, y:String) {
        print("\(x) \(y)")
    }

    return t
}

do {
    var t = testFunctionUsingTuple()
    t(1, "test")
}

do {
    var t = testFuncUsingArgs()
    t(1, "test")
}

Make the nested functions regular function instead and the same calling syntax for tuple no longer works:

func funcUsingTuple(x:(Int, String)) {
    print("\(x.0) \(x.1)")
}

func funcUsingArgs(x:Int, _ y:String) {
    print("\(x) \(y)")
}

// No longer works, need to call funcUsingTuple((1, "test")) instead
funcUsingTuple(1, "test")   
funcUsingArgs(1, "test3")

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/5919f2a6-327a-4c6f-8a9b-97c53739e306%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.