The problem here (IMO) is that this:
guard let `self` = self else { return }
isn't really boilerplate, but is important documentation about what the closure does — since its behavior in the face of self == nil
could reasonably be something more complicated, or sometimes could reasonably be handled later in the closure..
Given that, and that [guard self]
is problematic if the closure returns a value, it makes more sense to keep the actual guard
statement inside the closure, but look for ways to make it a little less wordy.
For example (and these are just suggestions to discuss, not a pitch), we can, and maybe already do — I can't remember if this was just talked about or actually done —, have this as official syntax:
guard let self = self else { return }
Next, I've always thought that single unconditional control-transferring statements (return
, break
, continue
, etc) should be syntactically equivalent to their singly { }
-ified selves, so we could have this:
guard let self = self else return
Finally, it might not be unreasonable to have a streamlined guard
syntax, either for shadowing variables generally, or just for self
specifically:
guard self else return // or maybe 'guard let self else return'
To me, a syntax like that eliminates enough boilerplate to make me happy, but keeps useful explicitness about how the nil value is handled.