[swift-evolution] Guaranteed closure execution

Gwendal Roué gwendal.roue at gmail.com
Sat Apr 23 18:21:36 CDT 2016

> Le 23 avr. 2016 à 18:33, Timothy Wood <tjw at me.com> a écrit :
>> 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.

I'm sorry but the goal of this proposal is not abstract, but very very very concrete:

	let x: Int
	f { x = 1 }
	// use x without compiler error

If you break this (and you do), you miss the point entirely.

> 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.

You are describing another proposal. Please please please please don't pollute this one.

Gwendal Roué

More information about the swift-evolution mailing list