[swift-evolution] [Pre-proposal] Replace [Foo] With CollectionType
matthew at anandabits.com
Tue May 24 18:01:32 CDT 2016
> On May 24, 2016, at 5:55 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>> Better support for existentials (see the generics manifesto, https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md) should obviate the need for any sort of sugar or compiler magic to do this kind of thing.
>> typealias AnyCollection<T> = Any<Collection where .Element == T, ...>
>> func doSomething(collection: AnyCollection<Foo>)
> That isn't really the same thing, though. Any<Collection> is an existential; it introduces indirection which would not be present in the generic version and, in this case, it erases several associated types, potentially introducing type-unsafety as well.
You are right about the indirection. I mentioned that Joe has talked about making generic arguments have identical behavior to existentials. I just realized that his comment was with respect to current existentials which don’t have associated types. It might also be possible to extend this to existentials which bind all associated types to concrete types, but it would not apply to something like AnyCollection<Foo> which leaves several associated types unbound. So as I mentioned, if we do introduce shorthand for something like AnyCollection we would need to decide whether the shorthand produces an implicit generic parameter or uses the existential.
I’m not sure what you mean about introducing type unsafely. The generalized existentials proposal goes out of its way to be explicit about the fact that only type safe operations would be visible through the existential.
> Brent Royal-Gordon
> swift-evolution mailing list
> swift-evolution at swift.org
More information about the swift-evolution