[swift-evolution] Changing the curried static methods

Erica Sadun erica at ericasadun.com
Sun Mar 13 21:54:46 CDT 2016


> If static methods had their arguments flipped, we'd be able to easily construct functions like `CGRect.insetBy(10.0, 10.0)` and `CGRect.offsetBy(-20.0, 10.0)`. These are now free functions without any mention to a specific rectangle. We could compose them to get a function that simulataneously insets and translates, all without mentioning a rectangle. With today's static methods we'd have to apply `flip` to each one, i.e. `flip(CGRect.insetBy)(10.0, 10.0)`.
> 
> This becomes very powerful with constructing data pipelines that describe how to transform data without ever actually mentioning the data. There are quite a few examples of things like this in Cocoa. A nice one is Core Image, in which you can build a pipeline of filters that you can feed images into.
> 

By your description, it sort of sounds like you could want to create a method cascade:

myRect
   ..insetBy(args)
   ..offsetBy(args)

In this, the instance passes through each of the function calls, joining them into a fluent declaration
with an implicit receiver.

But it also sounds like you want to use functional chaining. Core Image can chain because it always has 
inputImage and outputImage and set defaults:

myRect = myRect.insetBy(args).offsetBy(args)

Alternative, you could be doing some kind of weird chaining thing where so long as the output and inputs match
up they can be composed together:

f = (x: insetByAble).insetBy(args)->(T:offsetByAble).offsetBy(args)->U
myRect = f(myRect)

But in the end, it kind of almost really sounds like you just want to write a function

f(x: CGRect) -> CGRect {
   x.insetBy(10.0, 10.0)
   x.offsetBy(-20.0, 10.0)
   return x
}

Which of these are you aiming for?
            
-- E



More information about the swift-evolution mailing list