[swift-evolution] [Draft] open and public protocols
Matthew Johnson
matthew at anandabits.com
Sat Feb 18 16:26:47 CST 2017
> On Feb 18, 2017, at 3:43 PM, Charles Srstka <cocoadev at charlessoft.com> wrote:
>
>> On Feb 18, 2017, at 2:41 PM, Matthew Johnson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> There are good reasons a library may not wish to allow users to add conformances to a protocol. For example, it may not wish to expose the conforming concrete types. While similar behavior could be accomplished with an enum if cases could be private, that requires an implementation to use switch statements rather than polymorphism.
>
> Uh, what? You can expose protocols without exposing the concrete types that conform to them. That’s kind of the whole point of protocols.
>
> public protocol P {}
>
> private struct S: P {}
>
> public var p: P
>
> reduces to this interface:
>
> public protocol P {
> }
>
>
> public var p: P
>
> The library can then assign something of type S to the “p” variable, and clients will be able to use it without having access to its concrete type.
>
> Charles
>
Sorry, this part was not stated as clearly as it should have been. What intends to say is that the library may wish to not only hide the concrete types, but also control the set of types which may conform to the protocol (or be associated values of the enum) such that users cannot add to the set of types. I will update the document to more clearly describe this use case. Thanks for catching this!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170218/d1420481/attachment.html>
More information about the swift-evolution
mailing list