user1264176
user1264176

Reputation: 1128

Swift. Declaring private functions in internal protocol

How can I achieve something like this (doesn't compile):

internal protocol InternalPrivateMix {
    private func doPrivately()
    internal func doInternaly()
}

Basically I want to kind of make a promise that confirming class implements some functionality privately. This is more for self documentation. I obviously can just implement these functions in my classes without formally conform to protocol and write documentation describing that every class should implement this functionality. Though it would be nice if I could communicate my intent to other developers more formally.

EDIT: I have tried to implement two protocols in one file, one private, one internal as @creeperspeak suggested. However I cannot conform to private protocol in other files so it doesn't work.

Upvotes: 7

Views: 12889

Answers (2)

creeperspeak
creeperspeak

Reputation: 5523

From Apple's docs it looks like the only way to achieve what you are trying to do is to implement 2 protocols - one internal, and one private, as Apple states "You cannot set a protocol requirement to a different access level than the protocol it supports."

Upvotes: 8

Howard Lovatt
Howard Lovatt

Reputation: 1008

You can do this:

protocol P {
    func int()
}

extension P {
    func int() {
        print("int()")
        priv()
    }
    private func priv() {
        print("priv()")
    }
}

Which might serve your purpose - I use it.

Upvotes: 1

Related Questions