[swift-evolution] Apply for closure

James Campbell james at supmenow.com
Thu Jan 28 03:16:55 CST 2016


This is a discussion inspired by the recent proposal of Chris to remove turple splats.

I would like to propose we add a apply method for all closures.

What would the syntax look like?

To apply a bunch of variables to a normal function and call it we can first access it's closure like so

object.foo

Since this returns a closure to use apply we just do this

object.foo.apply(...)

When it comes to anonymous closures we can do the same:

closure.apply(...)

So why would we need this?

This is an alternative way of performing actions that you would normally do via NSInvocation and I feel it also opens the doors for things other people have proposed else where like partial initialisers. 

So how would the syntax work?

The compiler would allow you to pass in a array which will be splatted to have all of its arguments applied. This would be useful for applying vargs.

closure.apply(args:[12])

Since this is only applied to closures the compiler knows how many arguments an array should have and what value.

Additionally it could allow turples to replace the previous splat behviour

closure.apply(turple:(12))

My last is what I call a symbol hash:

closure.apply([
.argument: 12
])

It allows you to pass in dictionary to be used to construct the arguments.

The keys are a special generated symbol(enums maybe) that represent the argument label. The value for each key is passed to the relevant argument.

This allows you to construct a struct without having to pass all the parameters. Which is great for functional programming as you could allow people to  partially initialise a struct or to return a new copy with one value mutated (currently this requires a huge amount of boilerplate)

struct.copyWithMutation.apply([
.name: "hey"
])

I know nothing about compilers so would love to hear your thoughts. This makes the most sense to me coming from a ruby and JavaScript background 

Sent from my iPhone


More information about the swift-evolution mailing list