[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 12:29:29 CDT 2016

> Am 09.06.2016 um 00:18 schrieb Dave Abrahams via swift-evolution <swift-evolution at swift.org>:
> Exactly.  But much simpler cases will also either have to trap at
> runtime or be prohibited outright:
>  func subscript_<C: Collection>(c: C, i: C.Index) -> C.Collection.Element {
>    return c[i]
>  }

>  typealias IntCollection = Any<Collection where Element == Int>
>  let c1: IntCollection = ...
>  let c2: IntCollection = c1[3..<10]
>  let c3: IntCollection = ...
>  let c4: IntCollection = c1.reversed()
>  // Note: the underlying index types are the same, and are supposed to
>  // interoperate.  What do you do (work/trap/nocompile)?
>  _ = subscript_(c1, c2.startIndex)
>  // The underlying index types happen to be the same, and are not
>  // supposed to interoperate.  What do you do (silently “work”/trap/nocompile)?
>  _ = subscript_(c1, c3.startIndex)
>  // The underlying index types are different.  What do you do (trap/nocompile)?
>  _ = subscript_(c1, c4.startIndex)

All of these are type errors. All we know about c1, c2, c3 and c4 is that they are of type `Any<Collection where Element == Int>` which does not constrain the Index type, so all we know is that each variable can have a different Index type. So, the type system can only say, no, these are type errors.

On a second thought this example reads a little bit like the following:

let x1: Object = …
let x2: Object = „hello"

// work/trap/nocompile?

// x2 happens to be a String
// work/trap/nocompile?

I think we all agree that all these are type errors even though we know that x2 contains a String and it might be useful to work in some cases. Maybe :-)

The same applies to the examples above IMO. The static knowledge is not sufficient for those examples to compile.

But thinking of path dependent types the following should work:

let c1: IntCollection = …
let c2: c1.Type = c1[3..<10]

subscript_(c1, c2.startIndex) // ok!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160609/8d7ee34b/attachment.html>

More information about the swift-evolution mailing list