I think the problem here is that P == P is true, but P : P is not (a protocol 
does not conform to itself).

I think there was some discussion about it on the original "Completing 
Generics" thread from March. I'd probably ask on the swift-users list why P 
can't be made to conform to P, and then put together a proposal if there's no 
good reason.

Austin

> On May 29, 2016, at 3:13 PM, Charles Srstka <cocoa...@charlessoft.com> wrote:
> 
> Forgive me if this has already come up, but since we’re talking about fixing 
> generics, I wonder if there is any solution in the pipeline for this problem:
> 
> --
> 
> protocol P { func foo() }
> struct S: P { func foo() { print("foo") } }
> 
> func doSomething<C: CollectionType where C.Generator.Element: P>(c: C) {
>       for each in c {
>               each.foo()
>       }
> }
> 
> let arr: [P] = [S()]
> 
> doSomething(arr) // error: cannot invoke 'doSomething' with an argument list 
> of type '([P])’
> 
> --
> 
> Why is this an error? The whole definition of [P] is basically an array of 
> things that conform to P. Isn’t that exactly what “where Element: P” is 
> asking for?
> 
> Changing Element: P to Element == P solves this particular issue, of course, 
> but then passing something like [S] to the array will fail. The result is 
> that you need to write two functions, and either have one eat the performance 
> cost of constructing a new array that has the correct static type to pass to 
> the other (since, unlike arrays, I can’t figure out a way to convert 
> “Collection where Element: P” into “Collection where Element == P” with a 
> simple cast), or just fill it with the dreaded copy-paste code. Neither seems 
> ideal.
> 
> Charles
> 

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to