[swift-evolution] [Draft] open and public protocols
Charles Srstka
cocoadev at charlessoft.com
Sat Feb 18 15:43:42 CST 2017
> On Feb 18, 2017, at 2:41 PM, Matthew Johnson via swift-evolution <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170218/a295a6b7/attachment.html>
More information about the swift-evolution
mailing list