[swift-evolution] [Proposal] Automating Partial Application via Wildcards

Erica Sadun erica at ericasadun.com
Tue Feb 2 17:03:49 CST 2016


In current Swift,  this approach does not preserve argument labels :

let partial1 = { projectFunctionToCoordinateSystem(function: mySinFunction, p0: p0, p1: p1, x: $0) } // (CGFloat) -> CGPoint
let partial2 = { projectFunctionToCoordinateSystem(function: mySinFunction, p0: .zero, p1: $0, x: $1) } // (CGPoint, CGFloat) -> CGPoint


must be called partial1(0.5), not partial1(x: 0.5), unless you go to some significant work, as in the following:

let partial1: (x: CGFloat) -> CGPoint = {
    projectFunctionToCoordinateSystem(function: mySinFunction, p0: p0, p1: p1, x: $0) 
}

It's not that this is undoable, it's just ugly and laborious, things that are un-Swifty.  You must decide a priori which arguments will be applied, and manually name those that will not.

Compare with, for example, an imaginary version that uses $$ to accomplish the same with less planning and greater win:

let partial1 = projectFunctionToCoordinateSystem(function: mySinFunction, p0: p0, p1: p1, x: $$) // (x: CGFloat) -> CGPoint

I find it a lot cleaner, less crufty™ and nicer to use than jerry-rigging with a closure.

-- E

> On Feb 2, 2016, at 3:40 PM, Howard Lovatt via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Much prefer the syntax suggested by Greg that uses {} and $n
> 
> On Wednesday, 3 February 2016, Greg Titus via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> What about $N?  :-)  
> 
> And then you could also put {} around it so that it is more obvious that partial1 is being assigned something that acts like a closure, and then you end up with:
> 
> let partial1 = { projectFunctionToCoordinateSystem(function: mySinFunction, p0: p0, p1: p1, x: $0) }
> let partial2 = { projectFunctionToCoordinateSystem(function: mySinFunction, p0: .zero, p1: $0, x: $1) }
> 
> instead of:
>> let partial1 = projectFunctionToCoordinateSystem(function: mySinFunction, p0: p0, p1: p1, x: _) 
>> let partial2 = projectFunctionToCoordinateSystem(function: mySinFunction, p0: .zero, p1: _, x: _) 
> 
> I’m -1.
> 
> With the way that single expression closures work, and the fact that they are so syntactically light, I don’t think that this proposal would add utility, it would just be one more construct to learn.
> 
> 	- Greg
> 
>> On Feb 2, 2016, at 10:28 AM, Gwendal Roué via swift-evolution <swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
>> 
>> What about nothing?
>> 
>> let partial1 = f(arg1: 1, arg2:)
>> Gwendal
>> 
>>> Le 2 févr. 2016 à 19:26, Gwendal Roué <gwendal.roue at gmail.com <javascript:_e(%7B%7D,'cvml','gwendal.roue at gmail.com');>> a écrit :
>>> 
>>> Any operator character would be bad, since the function may accept it, as in `[1,2,3].reduce(1, combine: *)`
>>> 
>>> Gwendal
>>> 
>>>> Le 2 févr. 2016 à 19:24, Erica Sadun via swift-evolution <swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> a écrit :
>>>> 
>>>> 
>>>>> On Feb 2, 2016, at 11:20 AM, Chris Lattner <clattner at apple.com <javascript:_e(%7B%7D,'cvml','clattner at apple.com');>> wrote:
>>>>> 
>>>>> On Feb 2, 2016, at 10:16 AM, Erica Sadun <erica at ericasadun.com <javascript:_e(%7B%7D,'cvml','erica at ericasadun.com');>> wrote:
>>>>>>> 
>>>>>>> One superficial comment on this: the use of _ here is a bad idea.  _ already means something in expressions - “discard”, and a closely related thing in declarations - “ignore”.  
>>>>>>> 
>>>>>>> Adding a third very different thing (placeholder to be filled in later) seems like a really confusing thing to do.
>>>>>>> 
>>>>>>> -Chris
>>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> #?
>>>>> 
>>>>> # means “macro like” or “compiler synthesized”.
>>>>> 
>>>>> -Chris
>>>>> 
>>>> 
>>>> * would be bad, right? And naked ?-marks?
>>>> 
>>>> -- E
>>>> 
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> 
> -- 
>   -- Howard.
> 
> _______________________________________________
> 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/20160202/7da05dc5/attachment.html>


More information about the swift-evolution mailing list