Arrays of protocol conforming elements

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)?

Accepted Reply

In Apple's bug reporting system, you only have access to your own bugs. There's no way to search existing bugs or upvote them. However, some developers also post their bugs to "Open Radar" (Google it, if I post the link this message might get delayed in moderation). If you find the bug there, or you see a post here where someone says they reported the same bug and mentions their bug #, you could reference that bug number in your own bug report.


The Developer Tools category is OK for Swift bugs.


If you do report a bug, post a reply here with your bug #.

Replies

In Apple's bug reporting system, you only have access to your own bugs. There's no way to search existing bugs or upvote them. However, some developers also post their bugs to "Open Radar" (Google it, if I post the link this message might get delayed in moderation). If you find the bug there, or you see a post here where someone says they reported the same bug and mentions their bug #, you could reference that bug number in your own bug report.


The Developer Tools category is OK for Swift bugs.


If you do report a bug, post a reply here with your bug #.

Alright, thank you. I just reported bug #23615485.