[swift-evolution] Passing class protocols to functions accepting AnyObject

Anders Ha hello at andersio.co
Mon Dec 5 07:41:23 CST 2016


This is not a bug. Existentials of a protocol do not conform to the protocol. But I have no idea why the protocol conformance has not been implemented for at least protocols without `Self` or associated type requirements though.

Regards,
Anders

> On 5 Dec 2016, at 3:46 AM, Charles Srstka via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The following currently does not work:
> 
> protocol P: class {}
> class C: P {}
> 
> func foo<T>(t: T) where T: AnyObject {
> 	print("foo")
> }
> 
> let p: P = C()
> 
> foo(t: p) // error: cannot invoke 'foo' with an argument list of type '(t: P)'
> 
> It seems to me that this ought to have been allowed, since P is declared as being a reference type and thus should have been able to satisfy the function’s requirements.
> 
> Is this worthy of writing a language proposal, or would this be considered a bug that should be sent through the radar system instead?
> 
> Thanks,
> Charles
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list