Hello everyone!
I would like to propose adding partial application to Swift, allowing developers to create new functions by pre-filling some arguments of an existing function. I know that currying was removed, but I think we can consider this feature independently, as it brings a powerful addition to the Swift language.
This can enhance code reusability, and streamline function handling. The proposed syntax involves using a reserved character $
, but it could be another character or in another way for example&
. Let me explain with this function:
func foo(foo1: String, foo2: Int) -> [String]
let partialFoo = foo$(foo2: 5)
// Returns a function of type (String) -> [String]
But for functions without parameter names, partial application can be applied using positional placeholders:
var foo: (String, Int) -> [String]
let partialFoo = foo$($1: 5)
// Returns a function of type (String) -> [String]
I think that the use cases of this feature could be a lot giving to Swift a new posible approach for the functional programing.
Another posible case could be if you send all the parameters with the partial application, in that case you would obtain a () -> T function like this
let partialFoo = foo$("foo", 5)
// Returns a function of type () -> [String]
That would be useful for example in SwiftUI where you can send the parameters but as you get the void function you can send it to the action with only one more character and avoiding adding a new closure to execute it:
func foo(foo1: String, foo2: Int) -> Void
Button("Some Text", action: foo$("foo", 5))
// Returns a function of type () -> Void and is sent to the action
Thank you, everybody, for the incredible work that you have been doing over the past 10 years!
Update
Another alternative way could be with a reserved word like partial, for example:
let partialFoo = partial foo("foo", 5)
// Returns a function of type () -> [String]
Thanks to @crontab for the suggestion