Swift should have a built-in syntax for currying arbitrary functions

Originator:brent
Number:rdar://17414833 Date Originated:22-Jun-2014 10:53 PM
Status:Duplicate/17117798 Resolved:
Product:Developer Tools Product Version:Xcode6-Beta2 (6A216f)
Classification:Feature (New) Reproducible:Always
 
Summary:
Swift should allow you to curry any parameter of any function by writing a function call that explicitly omits some of the parameters. It should also work with operators.

Steps to Reproduce:
1. Create a playground.

2. Type the following:

    import Foundation
    let fiveRange = NSMakeRange(_, 5)    

Expected Results:
fiveRange becomes a function with signature (Int) -> (NSRange). When the resulting closure is called with a parameter x, it is equivalent to calling NSMakeRange(x, 5).

Actual Results:
An error: ‘_’ can only appear in a pattern or on the left side of an assignment

Notes:
In other languages, currying only allows you to fill in the leftmost parameters of a function, but Swift should be more flexible and allow you to fill in any parameter. Parameters given as _, on the other hand, should be preserved exactly as they are, with any keyword intact and in the same order. Defaulted parameters that are not explicitly mentioned should be accessible on the curried function as well, but should still have a default value. It should also be possible to specify a method’s receiver as _, in which case the curried function would either take self as the first parameter, or return a closure of type Self -> OtherParams -> ReturnType.

In some cases, using _ may not give enough type information to determine which overloaded version of a function you want to curry. In that case, you should be able to say, for instance, “_ as String”. This may be nearly mandatory when leaving self to be provided later.

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!