[swift-evolution] Public classes with private superclass

Haravikk swift-evolution at haravikk.me
Tue Jul 5 05:00:40 CDT 2016


> On 5 Jul 2016, at 08:01, Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
> 
> IMHO these things can be solved by private protocols with default implementation to reuse the code:
> Or you can just create a private class and use its instance in your classes:

The other alternative is to just declare the methods of the "private" class private/internal and then use them. For example:

	public class Foo {
		internal init() { … } // Can't be instantiated outside of this module
		internal func doSomething() { … }
		func publicMethod() { … }
	}

	public class Bar : Foo {
		public init() { super.init() } // Can be instantiated outside of this module
		public something() { super.doSomething() }
	}

But yeah, it seems like there are a lot of design patterns to solve this problem already; it's a nice idea, but proper abstract classes or traits/mixins are better possible solutions in the long-run, and the use of internal initialisers effectively result in abstract internal types for the time being.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160705/a061cb9d/attachment.html>


More information about the swift-evolution mailing list