[swift-evolution] [swift-evolution-announce] [Review] SE-0089: Replace protocol<P1, P2> syntax with Any<P1, P2>
Thorsten Seitz
tseitz42 at icloud.com
Thu Jun 9 11:23:17 CDT 2016
> Am 08.06.2016 um 23:37 schrieb Austin Zheng via swift-evolution <swift-evolution at swift.org>:
>
> func doSomething<T : Collection where T.Element == Int>(x: T, y: T) {
> // Get indexes out of x and use them to index into y
> var idx = x.startIndex
> while (idx != x.endIndex || idx != y.endIndex) {
> print(x[idx])
> print(y[idx])
> idx = x.nextIndex(idx)
> }
> }
> let someSeq : Any<Collection where .Element == Int> = // ...
> let anotherSeq : Any<Collection where .Element == Int> = // ...
> // Trouble!
> // someSeq and anotherSeq are the same existential type
> // But the concrete index types within each of the existential variables may be different
> doSomething(someSeq, anotherSeq)
>
> It's this situation (using an existential type to fulfill a generic type parameter constrained to the same requirements that comprise that existential) that requires either of the two options that Dave presented, due to our lack of compile-time type information about the fulfilling type's associated types.
>
Ok, that’s indeed a type error and I think that I now see why Any<Collection where .Element == Int> does not conform to T: Collection where T.Element == Int. This is because T stands for one *fixed* type out of the set of types described by Any<Collection where .Element == Int> whereas Any<Collection where .Element == Int> stands for *all* of them, which is more, so it cannot conform to T.
So doSomething(someSeq, anotherSeq) would be a type error and I’m fine with that.
We could try to write
func doSomething<T: Any<Collection where T.Element == Int>>(x: T, y: T) { … }
but then we wouldn’t be able to write the body as desired, because x.Index would be a different type from y.Index (and rightly so).
I think all this is fine, though. The above method doSomething() can only be written with runtime casts of the indices by opening them explicitly with one of the proposed syntaxes.
-Thorsten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160609/73c4ce8d/attachment.html>
More information about the swift-evolution
mailing list