beingadrian beingadrian - 9 months ago 74
Swift Question

Why do closures require an explicit `self` when they're all non-escaping by default in Swift 3?

I've noticed that in Swift 2.2, closures marked as non-escaping with

do not require an explicit
. In Swift 3, all closures are non-escaping by default and now requires them to be marked with
if you want them be able to escape.

Given that all of the closures in Swift 3 by default are non-escaping, why do they require an explicit

final class SomeViewController: NSViewController {

var someClosure: () -> () = { _ in }

override func viewDidLoad() {

someClosure = {
view.layer = CALayer() // ERROR: Implicit use of `self` in closure; use `self.` to make capture semantics explicit


Stored closures are considered escaping by default, even when they aren't really. There's no way to mark them non-escaping so we're stuck like this until they add @noescape back to the language, which they may or may not do. See this discussion on the swift-evolution mailing list.