[swift-evolution] [Review] SE-0026 Abstract classes and methods
joseph at human-friendly.com
Fri Feb 26 19:42:30 CST 2016
> On Feb 26, 2016, at 10:21 PM, David Scrève via swift-evolution <swift-evolution at swift.org> wrote:
> Actually, protocols and extensions cannot handle methods that requires attributes and data storage.
You don't implement the required code in the protocol/extension you provide a concrete instance conforming to the protocol.
> So, by design, protocols and extensions can only be used to implement stateless interface where abstract classes are true classes with a state supported with attributes and properties.
It is safer to share state only through the specific interface of the protocol.
> Abstract class can also enforce methods to be implemented and avoid misuse of default method implementation.
Require the object conforming to the protocol in the initializer.
> For example, NSOperation is a good candidat to abstract class because NSOperation itself is useless as a standalone class and should not be used as-it. -(void)main should be made abstract because I don’t think NSOperation does not have any internal property and has default behavior.
As mentioned it could just be initialised with a closure (or a simple delegate). With the delegate approach you could still subclass and just return self as the delegate if you really wanted to of some reason.
More information about the swift-evolution