[swift-evolution] Making capturing semantics of local functions explicit

C. Keith Ray keithray at mac.com
Wed Oct 25 18:39:24 CDT 2017


class A {
    func foo() -> ()->String {
        func local() -> String { [weak self] in 
            return "foo \(self.i)"
        }
        return local
    }
...

compares well with this...

class A {
    func foo() -> ()->String {
        let local : () -> String = { [weak self] in
            return "foo \(self?.i)"
        }
        return local
    }
    func foo2() -> ()->String {
        let local : () -> String = {
            return "foo2 \(self.i)"
        }
        return local
    }
    let i : Int
    init(_ i: Int) { self.i = i }
}
var a = A(2)
let b = a.foo()
a = A(4)
print(b()) // prints "foo nil"

let b2 = a.foo2()
a = A(6)
print(b2()) // prints "foo2 4"

--
C. Keith Ray

* https://leanpub.com/wepntk <- buy my book?
* http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf
* http://agilesolutionspace.blogspot.com/

> On Oct 25, 2017, at 1:21 PM, David Hart via swift-evolution <swift-evolution at swift.org> wrote:
> 
> class A { func foo() { func local() -> Int { [weak self] in } } }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171025/9b5374c5/attachment.html>


More information about the swift-evolution mailing list