[swift-evolution] [Manifesto] Completing Generics

Joe Groff jgroff at apple.com
Wed Mar 2 19:38:32 CST 2016


> On Mar 2, 2016, at 5:22 PM, Douglas Gregor via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Private conformances 
> 
> Right now, a protocol conformance can be no less visible than the minimum of the conforming type’s access and the protocol’s access. Therefore, a public type conforming to a public protocol must provide the conformance publicly. One could imagine removing that restriction, so that one could introduce a private conformance:
> 
> public protocol P { }
> public struct X { }
> extension X : internal P { … } // X conforms to P, but only within this module
> 
> The main problem with private conformances is the interaction with dynamic casting. If I have this code:
> 
> func foo(value: Any) {
>   if let x = value as? P { print(“P”) }
> }
> 
> foo(X())
> 
> Under what circumstances should it print “P”? If foo() is defined within the same module as the conformance of X to P? If the call is defined within the same module as the conformance of X to P? Never? Either of the first two answers requires significant complications in the dynamic casting infrastructure to take into account the module in which a particular dynamic cast occurred (the first option) or where an existential was formed (the second option), while the third answer breaks the link between the static and dynamic type systems—none of which is an acceptable result.

You don't need private conformances to introduce these coherence problems with dynamic casting. You only need two modules that independently extend a common type to conform to a common protocol. As Jordan discussed in his resilience manifesto, a publicly-subclassable base class that adopts a new protocol has the potential to create a conflicting conformance with external subclasses that may have already adopted that protocol. This seems to me like poor grounds for rejecting the ability to have private conformances. I think they're a really useful feature.

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160302/deec434e/attachment.html>


More information about the swift-evolution mailing list