[swift-evolution] Closure delegation

Pierre Monod-Broca pierremonodbroca at gmail.com
Fri Dec 11 05:27:12 CST 2015


> Le 11 déc. 2015 à 11:54, James Campbell <james at supmenow.com> a écrit :
> 
> Could you explain a little more its a bit confusing ?
> 
> On Fri, Dec 11, 2015 at 10:32 AM, Pierre Monod-Broca via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 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 -> () -> ()) {
Here we define a function `prepareSomething(_:)` which receives one parameter: a closure that takes a delegate conforming to `Bar`, and otherwise take no parameter and returns nothing

>     let bar = BarImplementation()
>     // code before
>     setup(bar)()
Here we pass a delegate to the closure, then call the closure

>     // code after
> }
> 
> prepareSomething { () -> () in
Here we call the function `prepareSomething(_:)` with a closure which we define a the same time

>     someConfig = "Hello world"
Here `someConfig` is a property of the closure’s delegate

> }
> 
> Where `someConfig` would refer to bar.
> 

I’m not sure about the syntax, we could also declare the delegate that way, maybe :
func prepareSomething(doSomething: @delegate(Bar) () -> ()) {
    /**/
}

> Pierre
> 
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> 
> 
> 
> -- 
>  Wizard
> james at supmenow.com <mailto:james at supmenow.com>
> +44 7523 279 698


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


More information about the swift-evolution mailing list