[swift-evolution] Casting Bug Swift

Joe Groff jgroff at apple.com
Thu Feb 25 13:50:50 CST 2016


> On Feb 25, 2016, at 11:45 AM, Radek Pietruszewski <radexpl at gmail.com> wrote:
> 
> Interesting. Two Qs:
> 
> - when you say "moving in the direction of encouraging the use of abstract AnyCollection values", do you mean in general? To encourage people to use AnyCollection<T> instead of [T] in their code?

Yeah. In Cocoa, the NSArray and NSDictionary class clusters serve the purpose not only of standard collections but as abstract interfaces for ordered and keyed collections, after all.

> - how could covariant collection casting look like in this world?

A no-language-support-needed approach might be to give the CollectionType protocol a `mapToType(T.self)` method which can be given specialized implementations for cases that can be efficiently implemented (such as mapping an array of class references to another class type), and/or fall back to wrapping with a lazy adapter in cases like going from a collection of ConcreteType to a collection of ProtocolType.

-Joe


More information about the swift-evolution mailing list