<div dir="ltr"><span style="font-size:12.8px">Hello,</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">(apologies if this got sent twice - gmail and Apple mail seems to confused as to what account the first mail was sent from)</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">I’m new to this mailing list, but have read some archived messages, and felt that this would be a reasonable subject to discuss. It’s somewhat related to the recent posts about @selfsafae/@guarded but distinctly different regardless.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Problem:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">It’s often desirable not to capture self in closures, but the syntax for doing so adds significant boilerplate code for [weak self] or us unsafe when used with [unowned self]. Typically you’d do something like this:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">  { [weak self] in    self?.execute() }</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">This is simple enough but often doesn’t work:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{ [weak self] in self?.boolean = self?.calculateBoolean() ]</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">This fails because boolean is not an optional. This in turn leads to code like this:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{ [weak self] in</span><br style="font-size:12.8px"><span style="font-size:12.8px">   guard let strongSelf = self else { return }</span><br style="font-size:12.8px"><span style="font-size:12.8px">   strongSelf.boolean = self.calculateBoolean()  }</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">And this is the boilerplate code. My suggestion is to add a syntax that works the same as the third syntax, yet doesn’t require the boilerplate code.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Solution:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Instead of using unowned or weak, let’s use guard/guarded syntax:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{ [guard self] in</span><br style="font-size:12.8px"><span style="font-size:12.8px">   self.isExecuted = self.</span><span style="font-size:12.8px">onlyIfWeakSelfWasCaptured<wbr>()</span><br style="font-size:12.8px"><span style="font-size:12.8px">}</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">In essence, guarded self is equivalent to a weak self, that’s captured when the closure is executed. If it was already released at that point, the closure is simply not executed. It’s equivalent to:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{ [weak self] in</span><br style="font-size:12.8px"><span style="font-size:12.8px">   guard let strongSelf = self else { return }</span><br style="font-size:12.8px"><span style="font-size:12.8px">   strongSelf.isExecuted = strongSelf.</span><span style="font-size:12.8px">onlyIfWeakSelfWasCa<wbr>ptured()</span><br style="font-size:12.8px"><span style="font-size:12.8px">}</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Except with a lot less boilerplate code, while not losing any clarify in what it does.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Impact / compatibility:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">This is simply additive syntax, and wouldn’t affect any existing code.</span><br></div>