[swift-users] Weird protocol behaviour.

Dave Abrahams dabrahams at apple.com
Thu Dec 22 16:17:23 CST 2016


on Thu Dec 22 2016, Mikhail Seriukov <swift-users-AT-swift.org> wrote:

> Hello community! I' wondering if somebody can explain this to me.
> Please take look at the snippet.
>
> protocol P {}
> struct X:P {}
>
> func foo<A:P>(_ x:A) {}
>
> func bar() {
>     //let x = X() // this compiles
>     let x = X() as P // this does not compile. Why?
>     foo(x)
> }

It's not a bug, exactly.  In Swift we have the bizarre situation that P
(when used as a type) doesn't conform to P (when used as a generic
constraint).  Doug can probably tell us if there's a good reason for
that or if it's just a temporary limitation.  Doug?

-- 
-Dave



More information about the swift-users mailing list