Swift 2 seems incapable of inferring protocol conformance from arrays. Example:
protocol Named {
var name: String { get }
}
struct Person: Named {
let name: String
var age: Int
}
func printNames(objects: [Named]) {
for object in objects {
print(object.name)
}
}
let person1 = Person(name: "Alice", age: 30)
let person2 = Person(name: "Bob", age: 40)
let persons = [person1, person2]
printNames([person1]) // Works
printNames([person1, person2]) // Works
printNames(persons) // Error: Cannot convert value of type '[Person]' to expected argument type '[Named]'
persons as? [Named] // Error: 'Named' is not a subtype of 'Person'
(My guess why lines 20 and 21 work is that the Swift compiler first looks at the method signature and then creates a [Named] array from the elements, whereas the persons array is already typed as [Person] (this would be consistent with the error in line 23), but that is just a side note.)
I know that there are workarounds for this that I'll use for now. I also know that I'm not the only one who walked into this problem (see for instance https://forums.developer.apple.com/thread/21540). However I think this is a bug and should be fixed in the future. So my question is what is the best way to make Apple aware of this issue? Can I find existing bug reports and "upvote" them? Or do I have to create my own bug report? In the bug reporter, there is no "Swift" category, so which category should I choose (maybe Developer Tools)?