[Swift] Allow down casting to child protocols

Originator:wirth_caesar
Number:rdar://18747958 Date Originated:23-Oct-2014
Status:Closed (Resolved) Resolved:
Product:Developer Tools Product Version:Xcode 6.1
Classification:Enhancement Reproducible:Always
 
Summary:
We can make protocols that extend other protocols. We can store items of the child protocol into collections that old objects of the parent protocol. However, when we get the objects out of the collection, it is impossible to treat it as anything other than an object of the parent protocol type.

I think it would be better if we could treat a child protocol object as a child protocol object, if it is a child protocol object. Sometimes you need to change behavior based on type.

Steps to Reproduce:
protocol Parent {}
protocol Child: Parent {}

let parentArray: [Parent] = []

for item in parentArray {
    if let child = item as Child { } // compile error
}

let childArray = parentArray.map { $0 as Child } // compile error
let otherArray = parentArray.filter { $0 is Child } // compile error

Expected Results:
That the above code would compile and run.
Although, I agree that the map should not work all the time --> not all Parents are Childs, so that should probably not work.

Actual Results:
Compile errors.

Version:
Version 6.1 (6A1052d)

Comments

Note to the reader:

This is duplicate of http://www.openradar.me/17833218 Which, in turn, is marked as a duplicate of 17328443

By wirth_caesar at Oct. 23, 2014, 8:53 a.m. (reply...)

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!