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
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users