[swift-evolution] Proposal: Allow `[strong self]` capture in closures and remove the `self` requirement therein

Brent Royal-Gordon brent at architechies.com
Tue Dec 15 17:14:29 CST 2015

> Swift currently requires that `self` be used explicitly inside closures, to help avoid bugs from unintentional capture. This is annoying when a closure uses `self` a lot. Closures should be allowed to name `[strong self]` in their capture list and thereafter not be required to write `self` everywhere.

Big +1 from me. I’ve been meaning to suggest this.

> What about [weak self] and [unowned self] ? I do not propose to change the `self` requirement for those closures. In the weak case it is critically important to know where `self` is accessed, because it could potentially become nil between any two accesses. Unowned self might be reasonable to change, but for simplicity I won't do so here.

`weak` is unfortunate, but your logic here makes sense; otherwise you’d have to write `map { $0.whatever() }`, which is even worse than `self?.whatever()`. I do think `unowned` makes sense to include, though.

Brent Royal-Gordon

More information about the swift-evolution mailing list