Add a new capture type for strong-weak-strong ownership
| Originator: | DeFrenZ | ||
| Number: | rdar://23410378 | Date Originated: | 05-Nov-2015 12:24 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 2.0 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
To avoid reference cycles sometimes you have to capture a weak reference in the capture list of a closure:
e.g.
{ [weak ref] in }
It is often the case that you want to reuse that same reference inside the closure multiple times, so one way is just to work with the `Optional` you will get:
e.g.
{ [weak ref] in
ref?.foo()
ref?.bar()
}
But you might want to ensure that once you start operating on it you do all the operations declared in the closure, so you do another strong reference before doing anything:
e.g.
{ [weak ref] in
guard let sref = ref else { return }
sref.foo()
sref.bar()
}
Since this pattern is quite common I would like some kind of system way to do it, like a new capturing type:
e.g.
{ [weakstrong ref] in
ref.foo()
ref.bar()
}
I can see the issue in determining what to do when the reference is actually lost, the cleanest (and easiest to manage) way to do it is maybe to force the closure to be optional and nil it when "weakstrong" captured values go nil. Other approaches are welcome as long as some boilerplate can be removed from this pattern
Expected Results:
Reduce the boilerplate in defining closures that don't strongly reference a property that they use but they will again once called
Actual Results:
Need to write another line every time (and declare a new variable in the case of self)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!