[swift-evolution] Guaranteed closure execution

Timothy Wood tjw at me.com
Sat Apr 23 11:33:55 CDT 2016



> On Apr 23, 2016, at 5:56 AM, Gwendal Roué via swift-evolution <swift-evolution at swift.org>
[...]
> 
> Since @once actually implies @noescape (as described earlier in the thread), it can be shortened to:
[...]

I'm surprised that @once would imply @noescape. In my opinion this makes it much less useful. For example, it is common to have asynchronous operations with a completion handler -- the completion handler will definitely escape and should definitely get called once.

In some cases the completion handler may get passed to other functions that take an @once block, so that should count as passing off your responsibility to call it.

In other cases, it may get stored in an object representing a long-lived async operation. This is trickier, but I would hope that a nullable var property, only in a reference type, could be marked @once. Reading that property would return an @once block *and* clear the property (so you'd be back to needing to call it or pass it away). It is unclear if only closure properties should be allowed to be marked @once, but presumable the implementation cloud support any type.

A simple noescape @once might find a few trivial uses in my codebase, but the really hard to diagnose bugs have been in async code where escaping is natural and necessary.

-tim


More information about the swift-evolution mailing list