[swift-evolution] [Draft] open and public protocols

Adrian Zubarev adrian.zubarev at devandartist.com
Sat Feb 18 16:00:14 CST 2017


Protocols solve way more problems than this. Sometimes they are used as backdoors to existing APIs as well. It’s simply an implementation artifact that such a protocol might end up public, but there is no point to allow the client to conform to it.

I also showed a concrete example in the discussion thread where I needed a public-but-not-open protocol really badly. ;)



-- 
Adrian Zubarev
Sent with Airmail

Am 18. Februar 2017 um 22:45:00, Charles Srstka via swift-evolution (swift-evolution at swift.org) schrieb:

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

_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170218/cb8c3ce6/attachment.html>


More information about the swift-evolution mailing list