[swift-evolution] Closure delegation

Pierre Monod-Broca pierremonodbroca at gmail.com
Fri Dec 11 04:32:31 CST 2015


A groovy closure can have a delegate which replaces `this` as the default receiver. The issue in groovy is that it is not compatible with static compilation, and there is no way to know from the code what is the type of the delegate.

It works great for DSL. It would work great the Swift Package Manager manifest, among other things.

It could look like this in swift

protocol Bar {
    var someConfig: String { get set }
}

func prepareSomething(setup: @delegate Bar -> () -> ()) {
    let bar = BarImplementation()
    // code before
    setup(bar)()
    // code after
}

prepareSomething { () -> () in
    someConfig = "Hello world"
}

Where `someConfig` would refer to bar.

Pierre

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/6b6a8d2f/attachment.html>


More information about the swift-evolution mailing list