Swift: Allow constructors to be referenced as functions
| Originator: | wetzeal | ||
| Number: | rdar://19997316 | Date Originated: | 28-Feb-2015 03:51 PM |
| Status: | Closed | Resolved: | 24-Jun-2015 |
| Product: | Developer Tools | Product Version: | Xcode-beta (6D532l) |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
Many functional languages treat constructors the same as any other function: they take a set of parameters of certain types, and return a single value of the expected type. This allows passing constructors anywhere where the parameter and return types match.
Switch treats types and constructors of those types as special non-functional entities. While their calling syntax looks like a function call, they can’t be referenced as functions themselves.
This means, for instance, that to convert an `Int?` to a `UInt?`, I must wrap the UInt constructor in a block: `maybeInt.map({ UInt($0)})`. It would be much nicer to more succinctly type `maybeInt.map(UInt)`, or at least, `maybeInt.map(UInt.init)`.
Steps to Reproduce:
let i: Int? = 200
let g = i.map(UInt.init)
Expected Results:
The code compiles
Actual Results:
“Initializer cannot be referenced without arguments”
Regression:
Many of the functional languages (or even not-particularly-functional, like Python) Swift has heritage from allow references initializers as functions.
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!