Swift should be able to infer inout types in closures

Originator:kristopherdjohnson
Number:rdar://21676609 Date Originated:05-Jul-2015 09:56 AM
Status:Duplicate/15998821 Resolved:
Product:Developer Tools Product Version:Xcode Version 7.0 beta (7A121l)
Classification:Enhancement Reproducible:Always
 
Summary:
In general, Swift can infer the types of parameters in closures when the function type is known. However, when the parameters are inout, it is necessary to explicitly specify those types.

Swift should be able to infer the types of these parameters.

Steps to Reproduce:
Enter this into a playground:

    let mySwap: (inout Int, inout Int) -> () = { a, b in
        (a, b) = (b, a)
    }
    
    var x = 1
    var y = 2

    mySwap(&x, &y)

    print("x = \(x), y = \(y)")

Expected Results:
Swift should be able to infer that the types of a and b in the closure are "inout Int".

Actual Results:
Compiler reports: error: '(_, _) -> ()' is not convertible to '(inout Int, inout Int) -> ()'
    (a, b) = (b, a)

To get it to compile, one must provide full explicit type annotations for the closure arguments, like this:

    let mySwap: (inout Int, inout Int) -> () = { (inout a: Int, inout b: Int) in
        (a, b) = (b, a)
    }


Version:
Xcode Version 7.0 beta (7A121l)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!