[swift-users] How to call a private protocol extension method from a public protocol extension method

David Ungar ungar at mac.com
Wed Jun 22 17:17:45 CDT 2016


Kevin,

Thank you so much for helping me out. I wasn’t clear, but I’m hoping to find a solution that uses only value types and protocols.

- David


> On Jun 22, 2016, at 1:19 PM, Kevin Greene <kgreenek at gmail.com> wrote:
> 
> You could have your common superclass implement your protocol. E.g...
> 
> public protocol PublicProto {
>   func publicFn()
> }
> 
> public class CommonSuper: PublicProto {
>   public func publicFn() { specificPrivate() }
>   private func specificPrivate() {}
> }
> 
> private class SubA: CommonSuper {
>   override private func specificPrivate() { /* ... */ }
> }
> 
> private class SubB: CommonSuper {
>   override  private func specificPrivate() { /* ... */ }
> }
> 
> I don't know what you're doing specifically, but I would guess that a cleaner approach would likely be to get rid of the super class entirely, and pull the shared logic from your two subclasses into a separate object that both classes instantiate, or have injected. Then have your two classes implement PublicProto directly. That discussion would probably be best had on another mailing list though.
> 
> On Wed, Jun 22, 2016 at 11:19 AM, David Ungar via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> I love protocol-oriented programming because of the guarantees that come with value types. But I cannot figure out how to do the same factoring I can do with the class side of the the language. I want to factor out common code into a public method that calls specific code in a private method & I want to do this for value types.
> 
> Here it is in classes:
> 
> public class CommonSuper {
>   public func publicFn() { … specificPrivateFn()  … }
>   private func specificPrivateFn() { }
> }
> 
> private class SubA {
>   override private func specificPrivate() { … }
> }
> private class SubB {
>   override  private func specificPrivate() { … }
> }
> 
> I have tried it lots of ways with protocols, and can get none to compile. Here is one:
> 
> public protocol PublicProto {
>     func publicFn()
> }
> 
> private protocol PrivateProto {
>     func specificPrivateFn()
> }
> 
> public extension  PublicProto where Self: PrivateProto { // Error: Extension cannot be declared public because its generic requirement uses a private type
>     public func publicFn() { specificPrivateFn() } // Error: Cannot declare a public instance method in an extension with private requirements
> }
> 
> private struct SA: PublicProto, PrivateProto {
>     private func specificPrivateFn() {}
> }
> private struct SB: PublicProto, PrivateProto {
>     private func specificPrivateFn() {}
> }
> 
> What am I doing wrong?
> 
> Thanks,
> 
> - David
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160622/47d0574a/attachment.html>


More information about the swift-users mailing list