[swift-evolution] [Pre-proposal] Replace [Foo] With CollectionType

Matthew Johnson matthew at anandabits.com
Tue May 24 20:04:49 CDT 2016


> On May 24, 2016, at 6:24 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> I’m not sure what you mean about introducing type unsafely.
> 
> What I mean is that once you do this:
> 
> 	let x: AnyCollection<Character> = myArrayOfCharacters
> 	let y: AnyCollection<Character> = myString.characters
> 
> Both `x` and `y` have indices of type `Any<Comparable>`, and will now accept each others' indices:
> 
> 	for i in x.indices {
> 		print(y[i])		// Oops!
> 	}
> 
> If this rule:
> 
>> 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.
> 
> Is trying to say that this isn't the case because APIs using the collection's `Index` are not exposed on an `AnyCollection`, well, then I'm not sure what `AnyCollection` is actually supposed to be used for.

Yeah, this is actually a good point.  We will probably have to implement the type-erased wrappers manually if we want this behavior.  I don’t know for sure but I imagine maybe it is considered acceptable in this case because you can already hit a fatal error with a bad array index anyway, so it isn’t totally unexpected.

> 
> -- 
> Brent Royal-Gordon
> Architechies
> 



More information about the swift-evolution mailing list