The topic of guarded closures came up recently in the thread The Future Of [weak self] Rebinding. I promised to look at the history of the topic and start a new thread for those interested in continuing the discussion. There have been two related proposal drafts in the past.
The first was for a relatively straightforward addition of a
guard capture specifier that would be available to closures returning
Void (and possibly
Optional). The draft can be found here: https://gist.github.com/emaloney/d34ac9b134ece7c60440 and the discussion thread is here: [Draft Proposal] A simplified notation for avoiding the weak/strong dance with closure capture lists.
The second draft is one I wrote a little over a year ago which explores introducing a new kind of closure which uses guarded captures by default (while still allowing an explicit capture list to override that default). The draft also introduces an
@guarded function argument attribute which can be used in place of
@escaping to require the argument to be a guarded closure. The intent behind this is to allow an API to indicate to users that correct use of the API will not usually extend the lifetime of an object and to make frequent usage patterns syntactically concise.
IIRC at the time I was also thinking about how to introduce guarded references to closures which would need to be
Optional and would be notified when one of the guards was released. This would allow a library to free up resources that are no longer needed when a callback becomes a no-op.
For a number of reasons I am less convinced that the additional complexity in my draft is worthwhile than I was a year ago. Additionally, introducing the basic
guard capture specifier is a step towards more sophisticated guarded closure features should we wish to introduce them in the future. What do others think? Is there a desire to pursue guarded closures in the Swift 5 timeframe and if so, which direction is preferable? Further, if there is a desire to pursue guarded closures is anyone interested in working on the implementation?