One of the most annoying parts of Swift is the [weak self] in / guard let strongSelf = self else { return } pattern.
Example:
doSomething() { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.doSomethingElse()
}
Having to write all this boilerplate code takes up time and adds noise to my code.
What I want is for the compiler to handle all this for me.
Why can't we enhance Swift in such a way that:
doSomething() { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf.doSomethingElse()
}
becomes something like:
doSomething() { [safe self] in
doSomethingElse()
}
I propose that [safe self] in
would automate the guard let strongSelf = self else { return }
pattern and remove the requirement of having to explicitly use self?
or strongSelf
.
Taking a crawl, walk, run approach, perhaps to start, [weak self] in
would only be useful when the closure return type is Void
?
If it's not too controversial, perhaps it can also be useful for closures returning an optional?
This would make me so happy!