Swift should have a syntax to retrieve a weakly-bound method
| Originator: | brent | ||
| Number: | rdar://17825602 | Date Originated: | 27-Jul-2014 11:05 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta4 (6A267n) |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
Swift provides a syntax to retrieve a closure that will call a given method: “obj.methodName”. With this syntax, however, “obj” is strongly referenced by the closure. There should be an easy syntax to retrieve a weakly-bound reference.
Steps to Reproduce:
1. Create a playground with the following prelude:
import Foundation
var obj = NSObject()
2. Attempt to retrieve a weakly-retaining closure for obj’s valueForKey(_:) method using the following idiomatic syntax:
let f = weak(obj).valueForKey
3. Attempt to retrieve a weakly-retaining closure for obj’s valueForKey(_:) method using the following low-impact syntax:
let g = NSObject.valueForKey(weak: obj)
Expected Results:
One of those approaches is valid Swift code.
Actual Results:
Neither is.
Notes:
It is possible to do this by explicitly creating a closure along the lines of “{ [weak obj] NSObject.valueForKey(obj!)($0) }”, but this is rather wordy (and handling the weakness safely would be even wordier). I think that at least #3, which introduces no new syntax and merely overloads the existing unbound method syntax, should be implemented. #2, which is significantly more readable, would be even better.
I’m not entirely sure what these closures should do if obj is deallocated before the closure is called. Perhaps the syntaxes proposed above should be altered to include a value to return in that circumstance. One small but important factor is that there needs to be a way for a Void function to remain Void even when bound to a weak target; this is critical if you want to replace uses of performSelector:, which is often used to call Void methods.
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!