Error messages when passing `init` methods at the type level are ambiguous and unhelpful
| Originator: | gordon | ||
| Number: | rdar://21861908 | Date Originated: | 16-Jul-2015 04:51 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Swift 2.0 |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
When passing `init` methods to higher order functions at the type level, you need to use `self.init` or else the compiler throws errors. However, the errors the compiler throws aren’t helpful and don’t tell you what the actual problem is.
Steps to Reproduce:
1. Create a simple struct:
```swift
struct Foo {
let foo: String
}
```
2. Add a type level method to the struct, and pass the init method without prefixing with `self`:
```swift
extension Foo {
static func bar(opt: String?) -> Foo? {
return opt.map(init)
}
}
```
Expected Results:
Expect to see a helpful error message that describes the actual problem (that the init method must be invoked on an explicit type)
Actual Results:
Multiple error messages (up to 8, with some duplications) are thrown at the same time, none of which are helpful:
- Consecutive statements on a line must be separated by ‘;’
- Expected ‘,’ separator
- Expected ‘(‘ for initializer parameters
- Expected expression in list of expressions
- Expected ‘)’ in expression list
- Expected expression
- Initializers may only be declared within a type
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!