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

David Ungar ungar at icloud.com
Wed Jun 22 13:19:05 CDT 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160622/60e7ba9b/attachment.html>


More information about the swift-users mailing list