# \[Proposal Update 1\] A simplified notation for avoiding the weak/strong dance with closure capture lists

**URL:** <https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415>\
**Category:** Discussion\
**Created:** [February 12, 2016, 6:23pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415 "2016-02-12T18:23:52Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_Maloney](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@E\_Maloney](https://forums.swift.org/u/E_Maloney)\
**Post date:** [February 12, 2016, 6:23pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/1 "2016-02-12T18:23:52Z")

</div>

Hello,

I sent out an earlier draft of this proposal last week, and have updated it based on feedback from the list.

The newer proposal now has a much smaller conceptual footprint to address concerns that the previous iteration was too complex and would lead to verbose notation within the capture lists.

You can find the updated gist here \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as pasted below.

Evan

> **···**
>
> ---
> 
> Simplified notation for avoiding the [weak self]/strongSelf dance with closures
> 
> Proposal: TBD  
> Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> Status: Draft  
> Review manager: TBD  
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt;Introduction)
> 
> Frequently, closures are used as completion callbacks for asynchronous operations, such as when dealing with network requests. It is quite common to model these sorts of operations in such a way that an object instance represents a request/response transaction, for example:
> 
> protocol NetworkTransaction: class  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> }  
> Here, the NetworkTransaction protocol declares the interface by which an asynchronous transaction occurs. The user of a NetworkTransaction calls the execute() function, passing in a completion function that is called at some time in the future, when the transaction completes.
> 
> For example, imagine a hypothetical DataConsumer class that uses a transaction to try to fetch some network data and process it:
> 
> class DataConsumer  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> 
> &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }  
> You'll notice the [weak self]/strongSelf dance in the fetchData() function. This is a common pattern with asynchronously-executed closures, and it signals the possibility that a closure might outlive its usefulness.
> 
> Because the NetworkTransaction may complete at any time, it is possible that the closure will execute after the DataConsumer that initiated the transaction has been deallocated. Perhaps the user has navigated elsewhere in the application and whatever data was to be fetched by DataConsumer is no longer needed.
> 
> In this case, after a DataConsumer instance goes away, we don't really want the closure doing anything. So, we capture self weakly to ensure that the closure doesn't hold a reference to the owning DataConsumer. That prevents a reference cycle and ensures that DataConsumer can be deallocated when no longer in use.
> 
> When it comes time to execute the closure, the guard statement effectively asks the question, "Is self still alive?" If the answer is no, the guard forces a return and the rest of the closure does not execute.
> 
> If self is still alive, then the weakly-captured self will be non-nil and it will be converted into a strong reference held by strongSelf for the duration of the closure's execution.
> 
> When the closure is done executing, strongSelf goes away, once again making the DataConsumer eligible for deallocation when no other references are held.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The) Problem
> 
> The [weak self]/strongSelf dance requires common boilerplate wherever it is used, and the fact that a self-like variable with an arbitrary name adds noise within the closure. The more strongSelf is needed within the closure, the more noise there is.
> 
> Further, using a consistent name like strongSelf is by convention only; it can't be enforced by the compiler, so searching your codebase for a given keyword won't be exhaustive if team members use the wrong name.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed) Solution
> 
> The proposed solution adds a new capture type by repurposing the guard keyword for another use, which would look like:
> 
> transaction.execute() { [guard self] result in  
> &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }  
> Here, the [guard self] capture list serves as a signal that the compiler should handle the weak/strong dance itself. When encountering [guard self], the compiler should emit code that does the following:
> 
> Captures self in a weak reference on behalf of the closure  
> Whenever the closure is about to be executed, the weak reference is checked to see if self is still alive  
> If self is not alive, the closure becomes a no-op \<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns immediately without anything inside the braces being executed  
> If self is alive, it is upgraded to a strong reference for the lifetime of the closure's execution. Within the closure, self is non-optional, unlike how it would be with a [weak self] capture. When the closure is done executing, the strong reference will be cleared and only the weak reference will be held on behalf of the closure.  
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt;Non%5C-self) References
> 
> Because guard is an additional capture type, like weak and unowned, it can also be used to capture references other than self:
> 
> let capturingTwo = { [guard self, button] in  
> &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> }  
> When encountering multiple references being captured via guard, the closure will execute only when all references are still alive when the closure is being asked to execute.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt;Limitations)
> 
> Because guard is a special capture type that causes the closure to become a no-op once a referenced object deallocates, it is only designed to be used with closures returning Void.
> 
> This limitation was deemed acceptable because it would cover the vast majority of cases, and those that it didn't cover can still fall back on the existing technique.
> 
> The compiler should emit an error if this notation is used in conjunction with a closure that has a non-Void return type.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> 
> This notation is not intended to be a full-fledged replacement for guard statements within the closure. We are only using guard here as a way to declare a specific memory-management behavior for references. Therefore, guard within [square brackets] should be seen as a capture type on par with weak or unowned.
> 
> Unlike with a typical guard statement, we are not attempting to support an else or where clause, or any boolean expressions within this notation.
> 
> Rather, we're simply adding a new capture behavior and providing a means to specify an early exit if the behavior couldn't be fulfilled because one or more objects was deallocated.
> 
> The word guard was chosen as the capture type because (1) it functions as a guard, ensuring that the closure doesn't execute unless the specified objects are still alive, and (2) it obviates the need for the full-fledged guard statement that would otherwise be required to achieve the same result.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact) on Existing Code
> 
> None, since this does not affect any existing constructs. Implementation of this proposal will not result in any code breakage.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives) Considered
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status) Quo
> 
> The primary alternative is to do nothing, requiring developers to add boilerplate guard code and handle upgrading the weak-to-strong references manually.
> 
> As stated above, this leads to needless boilerplate that can easily be factored out by the compiler. Also, the use of a self-like variable with an arbitrary name makes it more difficult to exhaustively find such uses in large projects. With this proposal, searching for the text "[guard" is all that's necessary to find all instances of this memory management technique.
> 
> Finally, the need to declare and use alternate names to capture values that already have existing names adds visual clutter to code and serves to obscure the code's original intent, making it harder to reason about.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures) with an Optional Return Type
> 
> One possible addition to this proposal would extend support to any closure returning an Optional of some kind.
> 
> If one of the objects in a guard capture list has been deallocated, executing the closure will always result in an immediate nil return.
> 
> This idea was excluded from this iteration of the proposal due to a concern that it relied on a "magic return value" (albeit a reasonable one) and a perception that the community favored a solution with a smaller conceptual footprint.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures) with a Bool Return Type
> 
> One possible addition to this proposal would extend support to any closure returning a Bool.
> 
> If one of the objects in a guard capture list has been deallocated, executing the closure will always result in an immediate false return.
> 
> This idea was excluded from this iteration of the proposal due to a concern that it relied on a "magic return value" (albeit a reasonable one) and a perception that the community favored a solution with a smaller conceptual footprint.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-arbitrary-return-types&gt;Closures) with Arbitrary Return Types
> 
> An earlier iteration of this proposal included support for closures with arbitrary return values. The community consensus was that the proposal was too heavy-weight and tried to do too much, and would lead to verbosity within the capture declaration. As a result, this idea was removed from the proposal.
> 
> The ability to handle non-Void return values relied on supporting an else clause within a guard-based capture list:
> 
> let happinessLevel: () -\> Int = { [guard self else -1] in  
> &nbsp;&nbsp;&nbsp;&nbsp;var level = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;level += self.isHealthy ? 25 : 0  
> &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isHungry ? 25 : 0  
> &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isFearful ? 25 : 0  
> &nbsp;&nbsp;&nbsp;&nbsp;level += self.hasLove ? 25 : 0  
> &nbsp;&nbsp;&nbsp;&nbsp;return level  
> }  
> Here, the else clause provides a value to return in cases where self has gone away and the guard fails.
> 
> In this example, if you call happinessLevel() after self has been deallocated, the value -1 will always be returned.
> 
> \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#citations&gt;Citations)
> 
> Variations on this proposal were discussed earlier in the following swift-evolution \<[https://lists.swift.org/mailman/listinfo/swift-evolution&gt](https://lists.swift.org/mailman/listinfo/swift-evolution&gt); threads:
> 
> Wanted: syntactic sugar for [weak self] callbacks \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt);  
> Allowing guard let self = self else { … } for weakly captured self in a closure. \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt);  
> [Draft Proposal] A simplified notation for avoiding the weak/strong dance with closure capture lists \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt);

---

<div class="post-metadata">

**Author:** ![David\_Turnbull](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@David\_Turnbull](https://forums.swift.org/u/David_Turnbull)\
**Post date:** [February 12, 2016, 6:41pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/2 "2016-02-12T18:41:09Z")

</div>

This could be the default behavior. A program can request "self", "weak  
self", or "unowned self" if they want something else.

-david

> **···**
>
> On Fri, Feb 12, 2016 at 10:23 AM, Evan Maloney via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > Hello,
> > 
> > I sent out an earlier draft of this proposal last week, and have updated  
> > it based on feedback from the list.
> > 
> > The newer proposal now has a much smaller conceptual footprint to address  
> > concerns that the previous iteration was too complex and would lead to  
> > verbose notation within the capture lists.
> > 
> > You can find the updated gist here  
> > \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as  
> > pasted below.
> > 
> > Evan
> > 
> > ---
> > 
> > Simplified notation for avoiding the [weak self]/strongSelf dance with  
> > closures
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: TBD  
> > &nbsp;&nbsp;&nbsp;- Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> > &nbsp;&nbsp;&nbsp;- Status: \*Draft\*  
> > &nbsp;&nbsp;&nbsp;- Review manager: TBD
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt);  
> > Introduction
> > 
> > Frequently, closures are used as completion callbacks for asynchronous  
> > operations, such as when dealing with network requests. It is quite common  
> > to model these sorts of operations in such a way that an object instance  
> > represents a request/response transaction, for example:
> > 
> > protocol NetworkTransaction: class  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> > }
> > 
> > Here, the NetworkTransaction protocol declares the interface by which an  
> > asynchronous transaction occurs. The user of a NetworkTransaction calls  
> > the execute() function, passing in a completion function that is called  
> > at some time in the future, when the transaction completes.
> > 
> > For example, imagine a hypothetical DataConsumer class that uses a  
> > transaction to try to fetch some network data and process it:
> > 
> > class DataConsumer  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > You'll notice the [weak self]/strongSelf dance in the fetchData() function.  
> > This is a common pattern with asynchronously-executed closures, and it  
> > signals the possibility that a closure might outlive its usefulness.
> > 
> > Because the NetworkTransaction may complete at any time, it is possible  
> > that the closure will execute after the DataConsumer that initiated the  
> > transaction has been deallocated. Perhaps the user has navigated elsewhere  
> > in the application and whatever data was to be fetched by DataConsumer is  
> > no longer needed.
> > 
> > In this case, after a DataConsumer instance goes away, we don't really  
> > want the closure doing anything. So, we capture self weakly to ensure  
> > that the closure doesn't hold a reference to the owning DataConsumer.  
> > That prevents a reference cycle and ensures that DataConsumer can be  
> > deallocated when no longer in use.
> > 
> > When it comes time to execute the closure, the guard statement  
> > effectively asks the question, "Is self still alive?" If the answer is  
> > no, the guard forces a return and the rest of the closure does not execute.
> > 
> > If self \*is\* still alive, then the weakly-captured self will be non-nil and  
> > it will be converted into a strong reference held by strongSelf for the  
> > duration of the closure's execution.
> > 
> > When the closure is done executing, strongSelf goes away, once again  
> > making the DataConsumer eligible for deallocation when no other  
> > references are held.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The)  
> > Problem
> > 
> > The [weak self]/strongSelf dance requires common boilerplate wherever it  
> > is used, and the fact that a self-like variable with an arbitrary name  
> > adds noise within the closure. The more strongSelf is needed within the  
> > closure, the more noise there is.
> > 
> > Further, using a consistent name like strongSelf is by convention only;  
> > it can't be enforced by the compiler, so searching your codebase for a  
> > given keyword won't be exhaustive if team members use the wrong name.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed)  
> > Solution
> > 
> > The proposed solution adds a new capture type by repurposing the guard keyword  
> > for another use, which would look like:
> > 
> > transaction.execute() { [guard self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Here, the [guard self] capture list serves as a signal that the compiler  
> > should handle the weak/strong dance itself. When encountering [guard self],  
> > the compiler should emit code that does the following:
> > 
> > &nbsp;&nbsp;&nbsp;- Captures self in a weak reference on behalf of the closure  
> > &nbsp;&nbsp;&nbsp;- Whenever the closure is about to be executed, the weak reference is  
> > &nbsp;&nbsp;&nbsp;checked to see if self is still alive  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is not alive, the closure becomes a no-op  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately without anything inside the braces being executed  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is alive, it is upgraded to a strong reference for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lifetime of the closure's execution. Within the closure, self is  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;non-optional, unlike how it would be with a [weak self] capture.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When the closure is done executing, the strong reference will be cleared  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and only the weak reference will be held on behalf of the closure.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt);  
> > Non-self References
> > 
> > Because guard is an additional capture type, like weak and unowned, it  
> > can also be used to capture references other than self:
> > 
> > let capturingTwo = { [guard self, button] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> > &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> > &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> > &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> > }
> > 
> > When encountering multiple references being captured via guard, the  
> > closure will execute \*only\* when \*all\* references are still alive when  
> > the closure is being asked to execute.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt);  
> > Limitations
> > 
> > Because guard is a special capture type that causes the closure to become  
> > a no-op once a referenced object deallocates, it is only designed to be  
> > used with closures returning Void.
> > 
> > This limitation was deemed acceptable because it would cover the vast  
> > majority of cases, and those that it didn't cover can still fall back on  
> > the existing technique.
> > 
> > The compiler should emit an error if this notation is used in conjunction  
> > with a closure that has a non-Void return type.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> > 
> > This notation is not intended to be a full-fledged replacement for guard  
> > statements within the closure. We are only using guard here as a way to  
> > declare a specific memory-management behavior for references. Therefore,  
> > guard within [square brackets] should be seen as a capture type on par  
> > with weak or unowned.
> > 
> > Unlike with a typical guard statement, we are not attempting to support  
> > an else or where clause, or any boolean expressions within this notation.
> > 
> > Rather, we're simply adding a new capture behavior and providing a means  
> > to specify an early exit if the behavior couldn't be fulfilled because one  
> > or more objects was deallocated.
> > 
> > The word guard was chosen as the capture type because (1) it functions as  
> > a guard, ensuring that the closure doesn't execute unless the specified  
> > objects are still alive, and (2) it obviates the need for the full-fledged  
> > guard statement that would otherwise be required to achieve the same  
> > result.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact)  
> > on Existing Code
> > 
> > None, since this does not affect any existing constructs. Implementation  
> > of this proposal will not result in any code breakage.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives)  
> > Considered  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status)  
> > Quo
> > 
> > The primary alternative is to do nothing, requiring developers to add  
> > boilerplate guard code and handle upgrading the weak-to-strong references  
> > manually.
> > 
> > As stated above, this leads to needless boilerplate that can easily be  
> > factored out by the compiler. Also, the use of a self-like variable with  
> > an arbitrary name makes it more difficult to exhaustively find such uses in  
> > large projects. With this proposal, searching for the text "[guard" is  
> > all that's necessary to find all instances of this memory management  
> > technique.
> > 
> > Finally, the need to declare and use alternate names to capture values  
> > that already have existing names adds visual clutter to code and serves to  
> > obscure the code's original intent, making it harder to reason about.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures)  
> > with an Optional Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning an Optional of some kind.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate nil return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures)  
> > with a Bool Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning a Bool.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate false return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-arbitrary-return-types&gt;Closures)  
> > with Arbitrary Return Types
> > 
> > An earlier iteration of this proposal included support for closures with  
> > arbitrary return values. The community consensus was that the proposal was  
> > too heavy-weight and tried to do too much, and would lead to verbosity  
> > within the capture declaration. As a result, this idea was removed from the  
> > proposal.
> > 
> > The ability to handle non-Void return values relied on supporting an else clause  
> > within a guard-based capture list:
> > 
> > let happinessLevel: () -\> Int = { [guard self else -1] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;var level = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.isHealthy ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isHungry ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isFearful ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.hasLove ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;return level  
> > }
> > 
> > Here, the else clause provides a value to return in cases where self has  
> > gone away and the guard fails.
> > 
> > In this example, if you call happinessLevel() after self has been  
> > deallocated, the value -1 will always be returned.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#citations&gt;Citations)
> > 
> > Variations on this proposal were discussed earlier in the following  
> > swift-evolution \<[https://lists.swift.org/mailman/listinfo/swift-evolution&gt](https://lists.swift.org/mailman/listinfo/swift-evolution&gt);  
> > threads:
> > 
> > &nbsp;&nbsp;&nbsp;- Wanted: syntactic sugar for [weak self] callbacks  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt);  
> > &nbsp;&nbsp;&nbsp;- Allowing guard let self = self else { … } for weakly captured self  
> > &nbsp;&nbsp;&nbsp;in a closure.  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt);  
> > &nbsp;&nbsp;&nbsp;- [Draft Proposal] A simplified notation for avoiding the weak/strong  
> > &nbsp;&nbsp;&nbsp;dance with closure capture lists  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt);
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Jason\_Gregori](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jason_gregori/32/15205_2.png) [@Jason\_Gregori](https://forums.swift.org/u/Jason_Gregori)\
**Post date:** [February 12, 2016, 7:02pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/3 "2016-02-12T19:02:42Z")

</div>

I like this much better 👍🏼.

Although I think adding weak in there would make what's going on much  
clearer. [guard self] looks more like it returns if self is nil. [guard  
weak self] obviously has something to do with weak semantics.

> **···**
>
> On Fri, Feb 12, 2016 at 10:44 AM, Erica Sadun via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > So long as it's technically possible, this would rock.
> > 
> > -- E
> > 
> > On Feb 12, 2016, at 11:23 AM, Evan Maloney \<emaloney@gilt.com\> wrote:
> > 
> > Hello,
> > 
> > I sent out an earlier draft of this proposal last week, and have updated  
> > it based on feedback from the list.
> > 
> > The newer proposal now has a much smaller conceptual footprint to address  
> > concerns that the previous iteration was too complex and would lead to  
> > verbose notation within the capture lists.
> > 
> > You can find the updated gist here  
> > \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as  
> > pasted below.
> > 
> > Evan
> > 
> > ---
> > 
> > Simplified notation for avoiding the [weak self]/strongSelf dance with  
> > closures
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: TBD  
> > &nbsp;&nbsp;&nbsp;- Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> > &nbsp;&nbsp;&nbsp;- Status: \*Draft\*  
> > &nbsp;&nbsp;&nbsp;- Review manager: TBD
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt);  
> > Introduction
> > 
> > Frequently, closures are used as completion callbacks for asynchronous  
> > operations, such as when dealing with network requests. It is quite common  
> > to model these sorts of operations in such a way that an object instance  
> > represents a request/response transaction, for example:
> > 
> > protocol NetworkTransaction: class  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> > }
> > 
> > Here, the NetworkTransaction protocol declares the interface by which an  
> > asynchronous transaction occurs. The user of a NetworkTransaction calls  
> > the execute() function, passing in a completion function that is called  
> > at some time in the future, when the transaction completes.
> > 
> > For example, imagine a hypothetical DataConsumer class that uses a  
> > transaction to try to fetch some network data and process it:
> > 
> > class DataConsumer  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > You'll notice the [weak self]/strongSelf dance in the fetchData() function.  
> > This is a common pattern with asynchronously-executed closures, and it  
> > signals the possibility that a closure might outlive its usefulness.
> > 
> > Because the NetworkTransaction may complete at any time, it is possible  
> > that the closure will execute after the DataConsumer that initiated the  
> > transaction has been deallocated. Perhaps the user has navigated elsewhere  
> > in the application and whatever data was to be fetched by DataConsumer is  
> > no longer needed.
> > 
> > In this case, after a DataConsumer instance goes away, we don't really  
> > want the closure doing anything. So, we capture self weakly to ensure  
> > that the closure doesn't hold a reference to the owning DataConsumer.  
> > That prevents a reference cycle and ensures that DataConsumer can be  
> > deallocated when no longer in use.
> > 
> > When it comes time to execute the closure, the guard statement  
> > effectively asks the question, "Is self still alive?" If the answer is  
> > no, the guard forces a return and the rest of the closure does not execute.
> > 
> > If self \*is\* still alive, then the weakly-captured self will be non-nil and  
> > it will be converted into a strong reference held by strongSelf for the  
> > duration of the closure's execution.
> > 
> > When the closure is done executing, strongSelf goes away, once again  
> > making the DataConsumer eligible for deallocation when no other  
> > references are held.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The)  
> > Problem
> > 
> > The [weak self]/strongSelf dance requires common boilerplate wherever it  
> > is used, and the fact that a self-like variable with an arbitrary name  
> > adds noise within the closure. The more strongSelf is needed within the  
> > closure, the more noise there is.
> > 
> > Further, using a consistent name like strongSelf is by convention only;  
> > it can't be enforced by the compiler, so searching your codebase for a  
> > given keyword won't be exhaustive if team members use the wrong name.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed)  
> > Solution
> > 
> > The proposed solution adds a new capture type by repurposing the guard keyword  
> > for another use, which would look like:
> > 
> > transaction.execute() { [guard self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Here, the [guard self] capture list serves as a signal that the compiler  
> > should handle the weak/strong dance itself. When encountering [guard self],  
> > the compiler should emit code that does the following:
> > 
> > &nbsp;&nbsp;&nbsp;- Captures self in a weak reference on behalf of the closure  
> > &nbsp;&nbsp;&nbsp;- Whenever the closure is about to be executed, the weak reference is  
> > &nbsp;&nbsp;&nbsp;checked to see if self is still alive  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is not alive, the closure becomes a no-op  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately without anything inside the braces being executed  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is alive, it is upgraded to a strong reference for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lifetime of the closure's execution. Within the closure, self is  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;non-optional, unlike how it would be with a [weak self] capture.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When the closure is done executing, the strong reference will be cleared  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and only the weak reference will be held on behalf of the closure.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt);  
> > Non-self References
> > 
> > Because guard is an additional capture type, like weak and unowned, it  
> > can also be used to capture references other than self:
> > 
> > let capturingTwo = { [guard self, button] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> > &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> > &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> > &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> > }
> > 
> > When encountering multiple references being captured via guard, the  
> > closure will execute \*only\* when \*all\* references are still alive when  
> > the closure is being asked to execute.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt);  
> > Limitations
> > 
> > Because guard is a special capture type that causes the closure to become  
> > a no-op once a referenced object deallocates, it is only designed to be  
> > used with closures returning Void.
> > 
> > This limitation was deemed acceptable because it would cover the vast  
> > majority of cases, and those that it didn't cover can still fall back on  
> > the existing technique.
> > 
> > The compiler should emit an error if this notation is used in conjunction  
> > with a closure that has a non-Void return type.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> > 
> > This notation is not intended to be a full-fledged replacement for guard  
> > statements within the closure. We are only using guard here as a way to  
> > declare a specific memory-management behavior for references. Therefore,  
> > guard within [square brackets] should be seen as a capture type on par  
> > with weak or unowned.
> > 
> > Unlike with a typical guard statement, we are not attempting to support  
> > an else or where clause, or any boolean expressions within this notation.
> > 
> > Rather, we're simply adding a new capture behavior and providing a means  
> > to specify an early exit if the behavior couldn't be fulfilled because one  
> > or more objects was deallocated.
> > 
> > The word guard was chosen as the capture type because (1) it functions as  
> > a guard, ensuring that the closure doesn't execute unless the specified  
> > objects are still alive, and (2) it obviates the need for the full-fledged  
> > guard statement that would otherwise be required to achieve the same  
> > result.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact)  
> > on Existing Code
> > 
> > None, since this does not affect any existing constructs. Implementation  
> > of this proposal will not result in any code breakage.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives)  
> > Considered  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status)  
> > Quo
> > 
> > The primary alternative is to do nothing, requiring developers to add  
> > boilerplate guard code and handle upgrading the weak-to-strong references  
> > manually.
> > 
> > As stated above, this leads to needless boilerplate that can easily be  
> > factored out by the compiler. Also, the use of a self-like variable with  
> > an arbitrary name makes it more difficult to exhaustively find such uses in  
> > large projects. With this proposal, searching for the text "[guard" is  
> > all that's necessary to find all instances of this memory management  
> > technique.
> > 
> > Finally, the need to declare and use alternate names to capture values  
> > that already have existing names adds visual clutter to code and serves to  
> > obscure the code's original intent, making it harder to reason about.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures)  
> > with an Optional Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning an Optional of some kind.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate nil return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures)  
> > with a Bool Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning a Bool.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate false return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-arbitrary-return-types&gt;Closures)  
> > with Arbitrary Return Types
> > 
> > An earlier iteration of this proposal included support for closures with  
> > arbitrary return values. The community consensus was that the proposal was  
> > too heavy-weight and tried to do too much, and would lead to verbosity  
> > within the capture declaration. As a result, this idea was removed from the  
> > proposal.
> > 
> > The ability to handle non-Void return values relied on supporting an else clause  
> > within a guard-based capture list:
> > 
> > let happinessLevel: () -\> Int = { [guard self else -1] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;var level = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.isHealthy ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isHungry ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isFearful ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.hasLove ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;return level  
> > }
> > 
> > Here, the else clause provides a value to return in cases where self has  
> > gone away and the guard fails.
> > 
> > In this example, if you call happinessLevel() after self has been  
> > deallocated, the value -1 will always be returned.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#citations&gt;Citations)
> > 
> > Variations on this proposal were discussed earlier in the following  
> > swift-evolution \<[https://lists.swift.org/mailman/listinfo/swift-evolution&gt](https://lists.swift.org/mailman/listinfo/swift-evolution&gt);  
> > threads:
> > 
> > &nbsp;&nbsp;&nbsp;- Wanted: syntactic sugar for [weak self] callbacks  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt);  
> > &nbsp;&nbsp;&nbsp;- Allowing guard let self = self else { … } for weakly captured self  
> > &nbsp;&nbsp;&nbsp;in a closure.  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt);  
> > &nbsp;&nbsp;&nbsp;- [Draft Proposal] A simplified notation for avoiding the weak/strong  
> > &nbsp;&nbsp;&nbsp;dance with closure capture lists  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt);
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [February 12, 2016, 6:44pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/4 "2016-02-12T18:44:30Z")

</div>

So long as it's technically possible, this would rock.

-- E

> **···**
>
> > On Feb 12, 2016, at 11:23 AM, Evan Maloney \<emaloney@gilt.com\> wrote:
> > 
> > Hello,
> > 
> > I sent out an earlier draft of this proposal last week, and have updated it based on feedback from the list.
> > 
> > The newer proposal now has a much smaller conceptual footprint to address concerns that the previous iteration was too complex and would lead to verbose notation within the capture lists.
> > 
> > You can find the updated gist here \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as pasted below.
> > 
> > Evan
> > 
> > ---
> > 
> > Simplified notation for avoiding the [weak self]/strongSelf dance with closures
> > 
> > Proposal: TBD  
> > Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> > Status: Draft  
> > Review manager: TBD  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt;Introduction)
> > 
> > Frequently, closures are used as completion callbacks for asynchronous operations, such as when dealing with network requests. It is quite common to model these sorts of operations in such a way that an object instance represents a request/response transaction, for example:
> > 
> > protocol NetworkTransaction: class  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> > }  
> > Here, the NetworkTransaction protocol declares the interface by which an asynchronous transaction occurs. The user of a NetworkTransaction calls the execute() function, passing in a completion function that is called at some time in the future, when the transaction completes.
> > 
> > For example, imagine a hypothetical DataConsumer class that uses a transaction to try to fetch some network data and process it:
> > 
> > class DataConsumer  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > You'll notice the [weak self]/strongSelf dance in the fetchData() function. This is a common pattern with asynchronously-executed closures, and it signals the possibility that a closure might outlive its usefulness.
> > 
> > Because the NetworkTransaction may complete at any time, it is possible that the closure will execute after the DataConsumer that initiated the transaction has been deallocated. Perhaps the user has navigated elsewhere in the application and whatever data was to be fetched by DataConsumer is no longer needed.
> > 
> > In this case, after a DataConsumer instance goes away, we don't really want the closure doing anything. So, we capture self weakly to ensure that the closure doesn't hold a reference to the owning DataConsumer. That prevents a reference cycle and ensures that DataConsumer can be deallocated when no longer in use.
> > 
> > When it comes time to execute the closure, the guard statement effectively asks the question, "Is self still alive?" If the answer is no, the guard forces a return and the rest of the closure does not execute.
> > 
> > If self is still alive, then the weakly-captured self will be non-nil and it will be converted into a strong reference held by strongSelf for the duration of the closure's execution.
> > 
> > When the closure is done executing, strongSelf goes away, once again making the DataConsumer eligible for deallocation when no other references are held.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The) Problem
> > 
> > The [weak self]/strongSelf dance requires common boilerplate wherever it is used, and the fact that a self-like variable with an arbitrary name adds noise within the closure. The more strongSelf is needed within the closure, the more noise there is.
> > 
> > Further, using a consistent name like strongSelf is by convention only; it can't be enforced by the compiler, so searching your codebase for a given keyword won't be exhaustive if team members use the wrong name.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed) Solution
> > 
> > The proposed solution adds a new capture type by repurposing the guard keyword for another use, which would look like:
> > 
> > transaction.execute() { [guard self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > Here, the [guard self] capture list serves as a signal that the compiler should handle the weak/strong dance itself. When encountering [guard self], the compiler should emit code that does the following:
> > 
> > Captures self in a weak reference on behalf of the closure  
> > Whenever the closure is about to be executed, the weak reference is checked to see if self is still alive  
> > If self is not alive, the closure becomes a no-op \<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns immediately without anything inside the braces being executed  
> > If self is alive, it is upgraded to a strong reference for the lifetime of the closure's execution. Within the closure, self is non-optional, unlike how it would be with a [weak self] capture. When the closure is done executing, the strong reference will be cleared and only the weak reference will be held on behalf of the closure.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt;Non%5C-self) References
> > 
> > Because guard is an additional capture type, like weak and unowned, it can also be used to capture references other than self:
> > 
> > let capturingTwo = { [guard self, button] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> > &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> > &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> > &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> > }  
> > When encountering multiple references being captured via guard, the closure will execute only when all references are still alive when the closure is being asked to execute.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt;Limitations)
> > 
> > Because guard is a special capture type that causes the closure to become a no-op once a referenced object deallocates, it is only designed to be used with closures returning Void.
> > 
> > This limitation was deemed acceptable because it would cover the vast majority of cases, and those that it didn't cover can still fall back on the existing technique.
> > 
> > The compiler should emit an error if this notation is used in conjunction with a closure that has a non-Void return type.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> > 
> > This notation is not intended to be a full-fledged replacement for guard statements within the closure. We are only using guard here as a way to declare a specific memory-management behavior for references. Therefore, guard within [square brackets] should be seen as a capture type on par with weak or unowned.
> > 
> > Unlike with a typical guard statement, we are not attempting to support an else or where clause, or any boolean expressions within this notation.
> > 
> > Rather, we're simply adding a new capture behavior and providing a means to specify an early exit if the behavior couldn't be fulfilled because one or more objects was deallocated.
> > 
> > The word guard was chosen as the capture type because (1) it functions as a guard, ensuring that the closure doesn't execute unless the specified objects are still alive, and (2) it obviates the need for the full-fledged guard statement that would otherwise be required to achieve the same result.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact) on Existing Code
> > 
> > None, since this does not affect any existing constructs. Implementation of this proposal will not result in any code breakage.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives) Considered
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status) Quo
> > 
> > The primary alternative is to do nothing, requiring developers to add boilerplate guard code and handle upgrading the weak-to-strong references manually.
> > 
> > As stated above, this leads to needless boilerplate that can easily be factored out by the compiler. Also, the use of a self-like variable with an arbitrary name makes it more difficult to exhaustively find such uses in large projects. With this proposal, searching for the text "[guard" is all that's necessary to find all instances of this memory management technique.
> > 
> > Finally, the need to declare and use alternate names to capture values that already have existing names adds visual clutter to code and serves to obscure the code's original intent, making it harder to reason about.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures) with an Optional Return Type
> > 
> > One possible addition to this proposal would extend support to any closure returning an Optional of some kind.
> > 
> > If one of the objects in a guard capture list has been deallocated, executing the closure will always result in an immediate nil return.
> > 
> > This idea was excluded from this iteration of the proposal due to a concern that it relied on a "magic return value" (albeit a reasonable one) and a perception that the community favored a solution with a smaller conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures) with a Bool Return Type
> > 
> > One possible addition to this proposal would extend support to any closure returning a Bool.
> > 
> > If one of the objects in a guard capture list has been deallocated, executing the closure will always result in an immediate false return.
> > 
> > This idea was excluded from this iteration of the proposal due to a concern that it relied on a "magic return value" (albeit a reasonable one) and a perception that the community favored a solution with a smaller conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-arbitrary-return-types&gt;Closures) with Arbitrary Return Types
> > 
> > An earlier iteration of this proposal included support for closures with arbitrary return values. The community consensus was that the proposal was too heavy-weight and tried to do too much, and would lead to verbosity within the capture declaration. As a result, this idea was removed from the proposal.
> > 
> > The ability to handle non-Void return values relied on supporting an else clause within a guard-based capture list:
> > 
> > let happinessLevel: () -\> Int = { [guard self else -1] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;var level = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.isHealthy ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isHungry ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isFearful ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.hasLove ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;return level  
> > }  
> > Here, the else clause provides a value to return in cases where self has gone away and the guard fails.
> > 
> > In this example, if you call happinessLevel() after self has been deallocated, the value -1 will always be returned.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#citations&gt;Citations)
> > 
> > Variations on this proposal were discussed earlier in the following swift-evolution \<[https://lists.swift.org/mailman/listinfo/swift-evolution&gt](https://lists.swift.org/mailman/listinfo/swift-evolution&gt); threads:
> > 
> > Wanted: syntactic sugar for [weak self] callbacks \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt);  
> > Allowing guard let self = self else { … } for weakly captured self in a closure. \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt);  
> > [Draft Proposal] A simplified notation for avoiding the weak/strong dance with closure capture lists \<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt);

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [February 12, 2016, 10:46pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/5 "2016-02-12T22:46:11Z")

</div>

> Alternatives Considered
> 
> Status Quo
> 
> Closures with an Optional Return Type
> 
> Closures with a Bool Return Type
> 
> Closures with Arbitrary Return Types

I would also discuss the alternative of allowing `self` to be shadowed so that you could write `guard let self = self else { return }`. Your proposal still has some advantages over that one, but I would like to see those explicitly called out and discussed.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![vmartinelli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vmartinelli/32/8443_2.png) [@vmartinelli](https://forums.swift.org/u/vmartinelli)\
**Post date:** [February 12, 2016, 7:39pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/6 "2016-02-12T19:39:41Z")

</div>

Well... While I think this could be a good idea, I think that the current  
implementation is already a “syntactic sugar” for the weak and strong  
dance. A simple `if let self = self` inside the closure (not allowed today)  
would be enough if we are going to change the current behaviour.

-Van

> **···**
>
> On Fri, Feb 12, 2016 at 4:23 PM, Evan Maloney via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > Hello,
> > 
> > I sent out an earlier draft of this proposal last week, and have updated  
> > it based on feedback from the list.
> > 
> > The newer proposal now has a much smaller conceptual footprint to address  
> > concerns that the previous iteration was too complex and would lead to  
> > verbose notation within the capture lists.
> > 
> > You can find the updated gist here  
> > \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as  
> > pasted below.
> > 
> > Evan
> > 
> > ---
> > 
> > Simplified notation for avoiding the [weak self]/strongSelf dance with  
> > closures
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: TBD  
> > &nbsp;&nbsp;&nbsp;- Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> > &nbsp;&nbsp;&nbsp;- Status: \*Draft\*  
> > &nbsp;&nbsp;&nbsp;- Review manager: TBD
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt);  
> > Introduction
> > 
> > Frequently, closures are used as completion callbacks for asynchronous  
> > operations, such as when dealing with network requests. It is quite common  
> > to model these sorts of operations in such a way that an object instance  
> > represents a request/response transaction, for example:
> > 
> > protocol NetworkTransaction: class  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> > }
> > 
> > Here, the NetworkTransaction protocol declares the interface by which an  
> > asynchronous transaction occurs. The user of a NetworkTransaction calls  
> > the execute() function, passing in a completion function that is called  
> > at some time in the future, when the transaction completes.
> > 
> > For example, imagine a hypothetical DataConsumer class that uses a  
> > transaction to try to fetch some network data and process it:
> > 
> > class DataConsumer  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > You'll notice the [weak self]/strongSelf dance in the fetchData() function.  
> > This is a common pattern with asynchronously-executed closures, and it  
> > signals the possibility that a closure might outlive its usefulness.
> > 
> > Because the NetworkTransaction may complete at any time, it is possible  
> > that the closure will execute after the DataConsumer that initiated the  
> > transaction has been deallocated. Perhaps the user has navigated elsewhere  
> > in the application and whatever data was to be fetched by DataConsumer is  
> > no longer needed.
> > 
> > In this case, after a DataConsumer instance goes away, we don't really  
> > want the closure doing anything. So, we capture self weakly to ensure  
> > that the closure doesn't hold a reference to the owning DataConsumer.  
> > That prevents a reference cycle and ensures that DataConsumer can be  
> > deallocated when no longer in use.
> > 
> > When it comes time to execute the closure, the guard statement  
> > effectively asks the question, "Is self still alive?" If the answer is  
> > no, the guard forces a return and the rest of the closure does not execute.
> > 
> > If self \*is\* still alive, then the weakly-captured self will be non-nil and  
> > it will be converted into a strong reference held by strongSelf for the  
> > duration of the closure's execution.
> > 
> > When the closure is done executing, strongSelf goes away, once again  
> > making the DataConsumer eligible for deallocation when no other  
> > references are held.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The)  
> > Problem
> > 
> > The [weak self]/strongSelf dance requires common boilerplate wherever it  
> > is used, and the fact that a self-like variable with an arbitrary name  
> > adds noise within the closure. The more strongSelf is needed within the  
> > closure, the more noise there is.
> > 
> > Further, using a consistent name like strongSelf is by convention only;  
> > it can't be enforced by the compiler, so searching your codebase for a  
> > given keyword won't be exhaustive if team members use the wrong name.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed)  
> > Solution
> > 
> > The proposed solution adds a new capture type by repurposing the guard keyword  
> > for another use, which would look like:
> > 
> > transaction.execute() { [guard self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Here, the [guard self] capture list serves as a signal that the compiler  
> > should handle the weak/strong dance itself. When encountering [guard self],  
> > the compiler should emit code that does the following:
> > 
> > &nbsp;&nbsp;&nbsp;- Captures self in a weak reference on behalf of the closure  
> > &nbsp;&nbsp;&nbsp;- Whenever the closure is about to be executed, the weak reference is  
> > &nbsp;&nbsp;&nbsp;checked to see if self is still alive  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is not alive, the closure becomes a no-op  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately without anything inside the braces being executed  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is alive, it is upgraded to a strong reference for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lifetime of the closure's execution. Within the closure, self is  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;non-optional, unlike how it would be with a [weak self] capture.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When the closure is done executing, the strong reference will be cleared  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and only the weak reference will be held on behalf of the closure.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt);  
> > Non-self References
> > 
> > Because guard is an additional capture type, like weak and unowned, it  
> > can also be used to capture references other than self:
> > 
> > let capturingTwo = { [guard self, button] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> > &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> > &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> > &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> > }
> > 
> > When encountering multiple references being captured via guard, the  
> > closure will execute \*only\* when \*all\* references are still alive when  
> > the closure is being asked to execute.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt);  
> > Limitations
> > 
> > Because guard is a special capture type that causes the closure to become  
> > a no-op once a referenced object deallocates, it is only designed to be  
> > used with closures returning Void.
> > 
> > This limitation was deemed acceptable because it would cover the vast  
> > majority of cases, and those that it didn't cover can still fall back on  
> > the existing technique.
> > 
> > The compiler should emit an error if this notation is used in conjunction  
> > with a closure that has a non-Void return type.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> > 
> > This notation is not intended to be a full-fledged replacement for guard  
> > statements within the closure. We are only using guard here as a way to  
> > declare a specific memory-management behavior for references. Therefore,  
> > guard within [square brackets] should be seen as a capture type on par  
> > with weak or unowned.
> > 
> > Unlike with a typical guard statement, we are not attempting to support  
> > an else or where clause, or any boolean expressions within this notation.
> > 
> > Rather, we're simply adding a new capture behavior and providing a means  
> > to specify an early exit if the behavior couldn't be fulfilled because one  
> > or more objects was deallocated.
> > 
> > The word guard was chosen as the capture type because (1) it functions as  
> > a guard, ensuring that the closure doesn't execute unless the specified  
> > objects are still alive, and (2) it obviates the need for the full-fledged  
> > guard statement that would otherwise be required to achieve the same  
> > result.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact)  
> > on Existing Code
> > 
> > None, since this does not affect any existing constructs. Implementation  
> > of this proposal will not result in any code breakage.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives)  
> > Considered  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status)  
> > Quo
> > 
> > The primary alternative is to do nothing, requiring developers to add  
> > boilerplate guard code and handle upgrading the weak-to-strong references  
> > manually.
> > 
> > As stated above, this leads to needless boilerplate that can easily be  
> > factored out by the compiler. Also, the use of a self-like variable with  
> > an arbitrary name makes it more difficult to exhaustively find such uses in  
> > large projects. With this proposal, searching for the text "[guard" is  
> > all that's necessary to find all instances of this memory management  
> > technique.
> > 
> > Finally, the need to declare and use alternate names to capture values  
> > that already have existing names adds visual clutter to code and serves to  
> > obscure the code's original intent, making it harder to reason about.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures)  
> > with an Optional Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning an Optional of some kind.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate nil return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures)  
> > with a Bool Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning a Bool.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate false return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-arbitrary-return-types&gt;Closures)  
> > with Arbitrary Return Types
> > 
> > An earlier iteration of this proposal included support for closures with  
> > arbitrary return values. The community consensus was that the proposal was  
> > too heavy-weight and tried to do too much, and would lead to verbosity  
> > within the capture declaration. As a result, this idea was removed from the  
> > proposal.
> > 
> > The ability to handle non-Void return values relied on supporting an else clause  
> > within a guard-based capture list:
> > 
> > let happinessLevel: () -\> Int = { [guard self else -1] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;var level = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.isHealthy ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isHungry ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += !self.isFearful ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;level += self.hasLove ? 25 : 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;return level  
> > }
> > 
> > Here, the else clause provides a value to return in cases where self has  
> > gone away and the guard fails.
> > 
> > In this example, if you call happinessLevel() after self has been  
> > deallocated, the value -1 will always be returned.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#citations&gt;Citations)
> > 
> > Variations on this proposal were discussed earlier in the following  
> > swift-evolution \<[https://lists.swift.org/mailman/listinfo/swift-evolution&gt](https://lists.swift.org/mailman/listinfo/swift-evolution&gt);  
> > threads:
> > 
> > &nbsp;&nbsp;&nbsp;- Wanted: syntactic sugar for [weak self] callbacks  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/008713.html&gt);  
> > &nbsp;&nbsp;&nbsp;- Allowing guard let self = self else { … } for weakly captured self  
> > &nbsp;&nbsp;&nbsp;in a closure.  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009023.html&gt);  
> > &nbsp;&nbsp;&nbsp;- [Draft Proposal] A simplified notation for avoiding the weak/strong  
> > &nbsp;&nbsp;&nbsp;dance with closure capture lists  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160201/009241.html&gt);
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Andrew](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew/32/949_2.png) [@Andrew](https://forums.swift.org/u/Andrew)\
**Post date:** [February 13, 2016, 8:45am UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/7 "2016-02-13T08:45:16Z")

</div>

Looks great, I support this as-is, I do have one suggestion, perhaps for a  
follow up proposal:

Have a protocol akin to NilLiteralConvertible, let's call it  
GuardFailureConvertible for now. Then have any failing guard able to  
implicitly return that value. The protocol is something like this:

protocol GuardFailureConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;init(guardFailure: Void)  
}

Your guard syntax could go unchanged, normal guards could also have a  
shorthand:

guard something() else return

> **···**
>
> On Saturday, 13 February 2016, Evan Maloney via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > Hello,
> > 
> > I sent out an earlier draft of this proposal last week, and have updated  
> > it based on feedback from the list.
> > 
> > The newer proposal now has a much smaller conceptual footprint to address  
> > concerns that the previous iteration was too complex and would lead to  
> > verbose notation within the capture lists.
> > 
> > You can find the updated gist here  
> > \<[https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;](https://gist.github.com/emaloney/d34ac9b134ece7c60440&gt;), as well as  
> > pasted below.
> > 
> > Evan
> > 
> > ---
> > 
> > Simplified notation for avoiding the [weak self]/strongSelf dance with  
> > closures
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: TBD  
> > &nbsp;&nbsp;&nbsp;- Author: Evan Maloney \<[https://github.com/emaloney&gt](https://github.com/emaloney&gt);  
> > &nbsp;&nbsp;&nbsp;- Status: \*Draft\*  
> > &nbsp;&nbsp;&nbsp;- Review manager: TBD
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#introduction&gt);  
> > Introduction
> > 
> > Frequently, closures are used as completion callbacks for asynchronous  
> > operations, such as when dealing with network requests. It is quite common  
> > to model these sorts of operations in such a way that an object instance  
> > represents a request/response transaction, for example:
> > 
> > protocol NetworkTransaction: class  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum Result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Succeeded(NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case Failed(ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func execute(completion: (Result) -\> Void)  
> > }
> > 
> > Here, the NetworkTransaction protocol declares the interface by which an  
> > asynchronous transaction occurs. The user of a NetworkTransaction calls  
> > the execute() function, passing in a completion function that is called  
> > at some time in the future, when the transaction completes.
> > 
> > For example, imagine a hypothetical DataConsumer class that uses a  
> > transaction to try to fetch some network data and process it:
> > 
> > class DataConsumer  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;let transaction: NetworkTransaction
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(transaction: NetworkTransaction)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transaction = transaction  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func fetchData()  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transaction.execute() { [weak self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard let strongSelf = self else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strongSelf.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func processData(data: NSData)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// process the data  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleError(error: ErrorType)  
> > &nbsp;&nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// handle the error  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > You'll notice the [weak self]/strongSelf dance in the fetchData() function.  
> > This is a common pattern with asynchronously-executed closures, and it  
> > signals the possibility that a closure might outlive its usefulness.
> > 
> > Because the NetworkTransaction may complete at any time, it is possible  
> > that the closure will execute after the DataConsumer that initiated the  
> > transaction has been deallocated. Perhaps the user has navigated elsewhere  
> > in the application and whatever data was to be fetched by DataConsumer is  
> > no longer needed.
> > 
> > In this case, after a DataConsumer instance goes away, we don't really  
> > want the closure doing anything. So, we capture self weakly to ensure  
> > that the closure doesn't hold a reference to the owning DataConsumer.  
> > That prevents a reference cycle and ensures that DataConsumer can be  
> > deallocated when no longer in use.
> > 
> > When it comes time to execute the closure, the guard statement  
> > effectively asks the question, "Is self still alive?" If the answer is  
> > no, the guard forces a return and the rest of the closure does not execute.
> > 
> > If self \*is\* still alive, then the weakly-captured self will be non-nil and  
> > it will be converted into a strong reference held by strongSelf for the  
> > duration of the closure's execution.
> > 
> > When the closure is done executing, strongSelf goes away, once again  
> > making the DataConsumer eligible for deallocation when no other  
> > references are held.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#the-problem&gt;The)  
> > Problem
> > 
> > The [weak self]/strongSelf dance requires common boilerplate wherever it  
> > is used, and the fact that a self-like variable with an arbitrary name  
> > adds noise within the closure. The more strongSelf is needed within the  
> > closure, the more noise there is.
> > 
> > Further, using a consistent name like strongSelf is by convention only;  
> > it can't be enforced by the compiler, so searching your codebase for a  
> > given keyword won't be exhaustive if team members use the wrong name.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#proposed-solution&gt;Proposed)  
> > Solution
> > 
> > The proposed solution adds a new capture type by repurposing the guard keyword  
> > for another use, which would look like:
> > 
> > transaction.execute() { [guard self] result in  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch result {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Succeeded(let data):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.processData(data)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Failed(let err):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.handleError(err)  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Here, the [guard self] capture list serves as a signal that the compiler  
> > should handle the weak/strong dance itself. When encountering [guard self],  
> > the compiler should emit code that does the following:
> > 
> > &nbsp;&nbsp;&nbsp;- Captures self in a weak reference on behalf of the closure  
> > &nbsp;&nbsp;&nbsp;- Whenever the closure is about to be executed, the weak reference is  
> > &nbsp;&nbsp;&nbsp;checked to see if self is still alive  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is not alive, the closure becomes a no-op  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://en.wikipedia.org/wiki/NOP&gt;](https://en.wikipedia.org/wiki/NOP&gt;); calling the closure returns  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately without anything inside the braces being executed  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- If self is alive, it is upgraded to a strong reference for the  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lifetime of the closure's execution. Within the closure, self is  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;non-optional, unlike how it would be with a [weak self] capture.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;When the closure is done executing, the strong reference will be cleared  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and only the weak reference will be held on behalf of the closure.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#non-self-references&gt);  
> > Non-self References
> > 
> > Because guard is an additional capture type, like weak and unowned, it  
> > can also be used to capture references other than self:
> > 
> > let capturingTwo = { [guard self, button] in  
> > &nbsp;&nbsp;&nbsp;&nbsp;// weakly capture self and button  
> > &nbsp;&nbsp;&nbsp;&nbsp;// but execute the closure with strong references  
> > &nbsp;&nbsp;&nbsp;&nbsp;// if and only if self AND button still exist  
> > &nbsp;&nbsp;&nbsp;&nbsp;// when the closure is being asked to execute  
> > }
> > 
> > When encountering multiple references being captured via guard, the  
> > closure will execute \*only\* when \*all\* references are still alive when  
> > the closure is being asked to execute.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#limitations&gt);  
> > Limitations
> > 
> > Because guard is a special capture type that causes the closure to become  
> > a no-op once a referenced object deallocates, it is only designed to be  
> > used with closures returning Void.
> > 
> > This limitation was deemed acceptable because it would cover the vast  
> > majority of cases, and those that it didn't cover can still fall back on  
> > the existing technique.
> > 
> > The compiler should emit an error if this notation is used in conjunction  
> > with a closure that has a non-Void return type.  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#caveats&gt;Caveats)
> > 
> > This notation is not intended to be a full-fledged replacement for guard  
> > statements within the closure. We are only using guard here as a way to  
> > declare a specific memory-management behavior for references. Therefore,  
> > guard within [square brackets] should be seen as a capture type on par  
> > with weak or unowned.
> > 
> > Unlike with a typical guard statement, we are not attempting to support  
> > an else or where clause, or any boolean expressions within this notation.
> > 
> > Rather, we're simply adding a new capture behavior and providing a means  
> > to specify an early exit if the behavior couldn't be fulfilled because one  
> > or more objects was deallocated.
> > 
> > The word guard was chosen as the capture type because (1) it functions as  
> > a guard, ensuring that the closure doesn't execute unless the specified  
> > objects are still alive, and (2) it obviates the need for the full-fledged  
> > guard statement that would otherwise be required to achieve the same  
> > result.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#impact-on-existing-code&gt;Impact)  
> > on Existing Code
> > 
> > None, since this does not affect any existing constructs. Implementation  
> > of this proposal will not result in any code breakage.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#alternatives-considered&gt;Alternatives)  
> > Considered  
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#status-quo&gt;Status)  
> > Quo
> > 
> > The primary alternative is to do nothing, requiring developers to add  
> > boilerplate guard code and handle upgrading the weak-to-strong references  
> > manually.
> > 
> > As stated above, this leads to needless boilerplate that can easily be  
> > factored out by the compiler. Also, the use of a self-like variable with  
> > an arbitrary name makes it more difficult to exhaustively find such uses in  
> > large projects. With this proposal, searching for the text "[guard" is  
> > all that's necessary to find all instances of this memory management  
> > technique.
> > 
> > Finally, the need to declare and use alternate names to capture values  
> > that already have existing names adds visual clutter to code and serves to  
> > obscure the code's original intent, making it harder to reason about.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-an-optional-return-type&gt;Closures)  
> > with an Optional Return Type
> > 
> > One possible addition to this proposal would extend support to any closure  
> > returning an Optional of some kind.
> > 
> > If one of the objects in a guard capture list has been deallocated,  
> > executing the closure will always result in an immediate nil return.
> > 
> > This idea was excluded from this iteration of the proposal due to a  
> > concern that it relied on a "magic return value" (albeit a reasonable one)  
> > and a perception that the community favored a solution with a smaller  
> > conceptual footprint.
> > 
> > \<[A simplified notation for avoiding the weak/strong dance with closure capture lists · GitHub](https://gist.github.com/emaloney/d34ac9b134ece7c60440#closures-with-a-bool-return-type&gt;Closures)  
> > with a Bool Return Type

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [February 12, 2016, 10:48pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/8 "2016-02-12T22:48:10Z")

</div>

Oh, and I just realized I have been responding to the wrong thread.

[weak self]  
guard reconstitute self else { return }

I thought it might be handy to control how the guard exits scope, enabling self == nil (as well as any other reconstitutions of a weak variable) failure cases to do reporting, fatalError-ing, etc. I think it's important to defer to the programmer on how the weak self condition is handled, enabling any clean-up action to be taken as well as deciding whether to terminate the app, recover, etc.

-- E, off to pick up kids

> **···**
>
> > On Feb 12, 2016, at 3:46 PM, Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > > Alternatives Considered
> > > 
> > > Status Quo
> > > 
> > > Closures with an Optional Return Type
> > > 
> > > Closures with a Bool Return Type
> > > 
> > > Closures with Arbitrary Return Types
> > 
> > I would also discuss the alternative of allowing `self` to be shadowed so that you could write `guard let self = self else { return }`. Your proposal still has some advantages over that one, but I would like to see those explicitly called out and discussed.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [February 12, 2016, 11:07pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/9 "2016-02-12T23:07:03Z")

</div>

> guard reconstitute self else { return }

I think `reconstitute` is inferior to `let self = ` on almost every axis. It's longer, requires you to learn a new keyword instead of applying existing constructs, and doesn't solve any other problems the way permitting `self` shadowing would.

> I think it's important to defer to the programmer on how the weak self condition is handled, enabling any clean-up action to be taken as well as deciding whether to terminate the app, recover, etc.

I'm really not convinced by this by this. If you want to terminate, capture the variable `unowned`. If you want to customize the behavior in some other way, do a weak-strong dance just like today.

I'm not entirely convinced `[guard]` is a good idea either, but the lack of `else` customization isn't the reason for my concern. "Just do nothing if this doesn't exist" is by far the most common case for guarding on a weak `self`.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [February 12, 2016, 11:41pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/10 "2016-02-12T23:41:00Z")

</div>

I'd be fine with let self = self. My biggest issue with [guard self] is that it has to happen before any defer.

-- E

> **···**
>
> > On Feb 12, 2016, at 4:07 PM, Brent Royal-Gordon \<brent@architechies.com\> wrote:
> > 
> > > guard reconstitute self else { return }
> > 
> > I think `reconstitute` is inferior to `let self = ` on almost every axis. It's longer, requires you to learn a new keyword instead of applying existing constructs, and doesn't solve any other problems the way permitting `self` shadowing would.
> > 
> > > I think it's important to defer to the programmer on how the weak self condition is handled, enabling any clean-up action to be taken as well as deciding whether to terminate the app, recover, etc.
> > 
> > I'm really not convinced by this by this. If you want to terminate, capture the variable `unowned`. If you want to customize the behavior in some other way, do a weak-strong dance just like today.
> > 
> > I'm not entirely convinced `[guard]` is a good idea either, but the lack of `else` customization isn't the reason for my concern. "Just do nothing if this doesn't exist" is by far the most common case for guarding on a weak `self`.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies

---

<div class="post-metadata">

**Author:** ![Javi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/javi/32/404_2.png) [@Javi](https://forums.swift.org/u/Javi)\
**Post date:** [February 13, 2016, 7:00am UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/11 "2016-02-13T07:00:15Z")

</div>

-1

I think "return and do anything if self is nil" is a code-smell in many  
cases. You're preventing a crash and a retain cycle, but at the expense of  
potentially leaving some piece of functionality broken. I think often  
strongly capturing self is OK if one can reason that the closure won't live  
forever, or use unowned if self is guaranteed to outlive the closure.  
Always resorting to weak self by default and without considering whether  
it's the right choice means not thinking carefully about the lifetime  
semantics of the code that you're writing, and adding syntax to make that  
easier would just encourage that.

> **···**
>
> On Fri, Feb 12, 2016 at 3:41 PM Erica Sadun via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > I'd be fine with let self = self. My biggest issue with [guard self] is  
> > that it has to happen before any defer.
> > 
> > -- E
> > 
> > \> On Feb 12, 2016, at 4:07 PM, Brent Royal-Gordon \<brent@architechies.com\> \> wrote:  
> > \>  
> > \>\> guard reconstitute self else { return }  
> > \>  
> > \> I think `reconstitute` is inferior to `let self = ` on almost every  
> > axis. It's longer, requires you to learn a new keyword instead of applying  
> > existing constructs, and doesn't solve any other problems the way  
> > permitting `self` shadowing would.  
> > \>  
> > \>\> I think it's important to defer to the programmer on how the weak self  
> > condition is handled, enabling any clean-up action to be taken as well as  
> > deciding whether to terminate the app, recover, etc.  
> > \>  
> > \> I'm really not convinced by this by this. If you want to terminate,  
> > capture the variable `unowned`. If you want to customize the behavior in  
> > some other way, do a weak-strong dance just like today.  
> > \>  
> > \> I'm not entirely convinced `[guard]` is a good idea either, but the lack  
> > of `else` customization isn't the reason for my concern. "Just do nothing  
> > if this doesn't exist" is by far the most common case for guarding on a  
> > weak `self`.  
> > \>  
> > \> --  
> > \> Brent Royal-Gordon  
> > \> Architechies  
> > \>
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> Javier Soto

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [February 13, 2016, 9:46am UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/12 "2016-02-13T09:46:43Z")

</div>

> I think "return and do anything if self is nil" is a code-smell in many cases. You're preventing a crash and a retain cycle, but at the expense of potentially leaving some piece of functionality broken.

You're breaking something unless you're not breaking anything. And I think it's actually pretty common that you're not breaking anything. For instance, if you perform an async network call to fetch the contents of a view controller and then dismiss the view controller before the fetch is complete, throwing away the results is often the best you can do. Even if you get an error, that error is probably no longer relevant.

Certainly not all blocks are like this—if you're \*writing\* data, you wouldn't want to throw away the result. But I think completion handlers which really can be safely ignored if the object that scheduled them has gone away are pretty common. Maybe not common enough to justify such a dramatic shorthand, but certainly common enough that I wouldn't call it a smell.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![Taras\_Zakharko1](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Taras\_Zakharko1](https://forums.swift.org/u/Taras_Zakharko1)\
**Post date:** [February 13, 2016, 11:07am UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/13 "2016-02-13T11:07:33Z")

</div>

-1

I like the concept but the proposed solution is inflexible and way too specific. Inability to deal with return values and also inability to execute behaviour if the references have led the scope are big no-goes IMO. This is adding more arbitrary magic to the language and making it more complex.

I am still favouring the

guard self = self else {}

solution. Slightly more verbose, but infinitely more flexible and makes it very clear what is going on.

— Taras

> **···**
>
> > On 13 Feb 2016, at 10:46, Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > > I think "return and do anything if self is nil" is a code-smell in many cases. You're preventing a crash and a retain cycle, but at the expense of potentially leaving some piece of functionality broken.
> > 
> > You're breaking something unless you're not breaking anything. And I think it's actually pretty common that you're not breaking anything. For instance, if you perform an async network call to fetch the contents of a view controller and then dismiss the view controller before the fetch is complete, throwing away the results is often the best you can do. Even if you get an error, that error is probably no longer relevant.
> > 
> > Certainly not all blocks are like this—if you're \*writing\* data, you wouldn't want to throw away the result. But I think completion handlers which really can be safely ignored if the object that scheduled them has gone away are pretty common. Maybe not common enough to justify such a dramatic shorthand, but certainly common enough that I wouldn't call it a smell.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Carlos\_Parada](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Carlos\_Parada](https://forums.swift.org/u/Carlos_Parada)\
**Post date:** [February 13, 2016, 9:27pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/14 "2016-02-13T21:27:43Z")

</div>

> guard self = self else {}

I agree with Taras on this. I also would prefer something along these lines. Besides being more consistent and familiar, the else clause encourages you to think what would happen if self is gone. "Is it fine if I just return or do I have to display an error message?” This helps lessens the points Javier Soto had against the original idea.

— Carlos Parada

> **···**
>
> > On Feb 13, 2016, at 3:07 AM, Taras Zakharko via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > -1
> > 
> > I like the concept but the proposed solution is inflexible and way too specific. Inability to deal with return values and also inability to execute behaviour if the references have led the scope are big no-goes IMO. This is adding more arbitrary magic to the language and making it more complex.
> > 
> > I am still favouring the
> > 
> > guard self = self else {}
> > 
> > solution. Slightly more verbose, but infinitely more flexible and makes it very clear what is going on.
> > 
> > — Taras
> > 
> > > On 13 Feb 2016, at 10:46, Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > > I think "return and do anything if self is nil" is a code-smell in many cases. You're preventing a crash and a retain cycle, but at the expense of potentially leaving some piece of functionality broken.
> > > 
> > > You're breaking something unless you're not breaking anything. And I think it's actually pretty common that you're not breaking anything. For instance, if you perform an async network call to fetch the contents of a view controller and then dismiss the view controller before the fetch is complete, throwing away the results is often the best you can do. Even if you get an error, that error is probably no longer relevant.
> > > 
> > > Certainly not all blocks are like this—if you're \*writing\* data, you wouldn't want to throw away the result. But I think completion handlers which really can be safely ignored if the object that scheduled them has gone away are pretty common. Maybe not common enough to justify such a dramatic shorthand, but certainly common enough that I wouldn't call it a smell.
> > > 
> > > --  
> > > Brent Royal-Gordon  
> > > Architechies
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [February 14, 2016, 3:31pm UTC](https://forums.swift.org/t/proposal-update-1-a-simplified-notation-for-avoiding-the-weak-strong-dance-with-closure-capture-lists/1415/15 "2016-02-14T15:31:07Z")

</div>

-1

Like was previously stated, I would prefer something like "guard let self = self else {}" to be valid.  
It has the benefit of:

- being less surprising  
- having an even smaller language footprint  
- fixing all the caveats of the original proposal

> **···**
>
> > On 13 Feb 2016, at 22:27, Carlos Parada via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > > guard self = self else {}
> > 
> > I agree with Taras on this. I also would prefer something along these lines. Besides being more consistent and familiar, the else clause encourages you to think what would happen if self is gone. "Is it fine if I just return or do I have to display an error message?” This helps lessens the points Javier Soto had against the original idea.
> > 
> > — Carlos Parada
> > 
> > > On Feb 13, 2016, at 3:07 AM, Taras Zakharko via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > -1
> > > 
> > > I like the concept but the proposed solution is inflexible and way too specific. Inability to deal with return values and also inability to execute behaviour if the references have led the scope are big no-goes IMO. This is adding more arbitrary magic to the language and making it more complex.
> > > 
> > > I am still favouring the
> > > 
> > > guard self = self else {}
> > > 
> > > solution. Slightly more verbose, but infinitely more flexible and makes it very clear what is going on.
> > > 
> > > — Taras
> > > 
> > > > On 13 Feb 2016, at 10:46, Brent Royal-Gordon via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > > I think "return and do anything if self is nil" is a code-smell in many cases. You're preventing a crash and a retain cycle, but at the expense of potentially leaving some piece of functionality broken.
> > > > 
> > > > You're breaking something unless you're not breaking anything. And I think it's actually pretty common that you're not breaking anything. For instance, if you perform an async network call to fetch the contents of a view controller and then dismiss the view controller before the fetch is complete, throwing away the results is often the best you can do. Even if you get an error, that error is probably no longer relevant.
> > > > 
> > > > Certainly not all blocks are like this—if you're \*writing\* data, you wouldn't want to throw away the result. But I think completion handlers which really can be safely ignored if the object that scheduled them has gone away are pretty common. Maybe not common enough to justify such a dramatic shorthand, but certainly common enough that I wouldn't call it a smell.
> > > > 
> > > > --  
> > > > Brent Royal-Gordon  
> > > > Architechies
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
