Swift: Cannot specialize methods on their type’s parameters
| Originator: | rix.rob | ||
| Number: | rdar://17429695 | Date Originated: | 23-Jun-2014 10:26 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta2 (6A216f) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary: Given Box<T>, I would like to make Box conform to Printable such that if T is Printable, Box uses the value’s description property; if it is Reflectable, it uses the reflect(value).summary; and otherwise it just makes something up. Unfortunately, despite the fact that this could (I think) be determined from a given T, I can’t (modulo the `as?` operator—see Notes, below). Further, calling a function which is generic on T doesn’t work either—if I provide describe() specializations for Printable, Reflectable, and arbitrary T, it always calls the arbitrary T one. Example code can be found here: https://gist.github.com/robrix/80b14ad4257f96c339e8 Steps to Reproduce: 1. Make a type which is generic on a parameter T. 2. Try to make it Printable in either manner described above. 3. Print an instance of it, whose value is of a type which conforms to Printable. Expected Results: I expected the resulting string to indicate that the code path had been selected based on the knowledge that T for this instance was Printable. Actual Results: Instead, the string was the arbitrary one. Regression: N/A Notes: This could be resolved by extensions parameterized on types, universal function-call syntax as described in rdar://17405411, or by specializing the call site based on the value of T. I do not think `as?` is viable due to the limitation requiring @objc (which isn’t used in the stdlib), and also due to the limitation prohibiting struct/enum/tuple checks.
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!