[swift-evolution] [Idea] How to eliminate 'optional' protocol requirements
Gwendal Roué
gwendal.roue at gmail.com
Wed Apr 13 12:13:19 CDT 2016
> On Apr 11, 2016, at 12:15 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
>
> To me, compound names for closure properties and satisfying property requirements with methods aren't hacks, they're missing features we ought to support anyway.
Hello,
I may have missed the point, but it looks like you say that optional delegate methods could be replaced by closures.
If this is the case, then I recently faced an issue with closures as a replacement for optional delegate methods, and the issue was with the responsibility of weak reference of the delegate.
So instead of:
protocol CDelegate : class { func f() }
class C {
weak var delegate: CDelegate?
func doIt() {
delegate?.f()
}
}
We’d have:
class C {
var f: (() -> ())?
func doIt() {
f?()
}
}
Is it what you were referring to?
If so, then the trouble is for the code that sets the closure. It has to perform the weak self/strongSelf dance:
c = C()
c.f = { [weak self] in
guard let strongSelf = self else { return }
strongSelf….
}
I find it awfully awfully heavy. The caller has 1. to remember about weakifying self, and 2. extract strongSelf from the weak self.
Of course, if you were talking about something else, you can discard my comment.
Gwendal Roué
More information about the swift-evolution
mailing list