I've noticed that in Swift 2.2, closures marked as non-escaping with @noescape
do not require an explicit self
. In Swift 3, all closures are non-escaping by default and now requires them to be marked with @escaping
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 self
?
final class SomeViewController: NSViewController {
var someClosure: () -> () = { _ in }
override func viewDidLoad() {
super.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.No, in Swift 3, only closure function arguments (i.e function inputs that are functions themselves) are non-escaping by default (as per SE-0103). For example:
As
closure
in the above example is non-escaping, it is prohibited from being stored or captured, thus limiting its lifetime to the lifetime of the functionfoo(_:)
. This therefore means that any values it captures are guaranteed to not remain captured after the function exits – meaning that you don’t need to worry about problems that can occur with capturing, such as retain cycles.However, a closure stored property (such as
bar
in the above example) is by definition escaping (it would be nonsensical to mark it with@noescape
) as its lifetime not limited to a given function – it (and therefore all its captured variables) will remain in memory as long as the given instance remains in memory. This can therefore easily lead to problems such as retain cycles, which is why you need to use an explicitself.
in order to make the capturing semantics explicit.In fact, case in point, your example code will create a retain cycle upon
viewDidLoad()
being called, assomeClosure
strongly capturesself
, andself
strongly referencessomeClosure
, as it's a stored property.It's worth noting that as an extension of the "stored function properties are always escaping" rule, functions stored in aggregates (i.e structures and enumerations with associated values) are also always escaping, as there's no restrictions on what you do with such aggregates. As pointed out by pandaren codemaster, this currently includes
Optional
– meaning thatOptional<() -> Void>
(aka.(() -> Void)?
) is always escaping. The compiler might eventually make this a special case for function parameters though, given that optional is already built on a lot of compiler magic.Of course, one place where you would expect to be able to use the
@noescape
attribute is on a closure that’s a local variable in a function. Such a closure would have a predictable lifetime, as long as it isn’t stored outside of the function, or captured. For example:Unfortunately, as
@noescape
is being removed in Swift 3, this won't be possible (What's interesting is that in Xcode 8 GM, it is possible, but yields a deprecation warning). As Jon Shier says, we’ll have to wait for it to be re-added to the language, which may or may not happen.