<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Not sure the language direction should <i class="">encourage</i> inheritance based structures, which abstract classes and methods do. That’s not to say that inheritance is dead, but that a modern language should encourage and support compositional patterns rather than inheritance based patterns.&nbsp;<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 7, 2016, at 7:19 PM, charles--- via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">+1<br class=""><br class="">I have loads of "pretend abstract" classes littered with stuff like this:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;var boo:Bool! { return nil /*DUMMY*/ }<br class=""><br class="">It takes a significant amount of energy atm to plan a Swift project because there are quirky differences between: protocol / subclass / class extension. It's not so straightforward to pick the most appropriate one.<br class=""><br class=""><br class=""><br class="">Sent from my iPhone<br class=""><br class=""><blockquote type="cite" class="">On Jan 7, 2016, at 9:55 AM, David Scrève via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""># Abstract classes and methods<br class=""><br class="">* Author(s): David Scrève<br class=""><br class="">## Introduction<br class=""><br class="">When developing framework and reusable, we need to develop classes that are partially <br class="">abstract with partial implementation. Protocol and protocol extensions provide this, but <br class="">they cannot have attributes as classes have.<br class="">A partial class combines the behavior of a class with the requirement of implementing methods<br class="">in inherited class like protocols.<br class=""><br class="">Swift-evolution thread: [link to the discussion thread for that proposal](<a href="https://lists.swift.org/pipermail/swift-evolution" class="">https://lists.swift.org/pipermail/swift-evolution</a>)<br class=""><br class="">## Motivation<br class="">Like pure virtual methods in C++ and abtract classes in Java and C#, frameworks development <br class="">sometimes required abstract classes facility.<br class="">An abstract class is like a regular class, but some methods/properties are not implemented <br class="">and must be implemented in one of inherited classes.<br class="">An abstract class can inherit from other class, implements protocols and has members <br class="">attributes as opposite from protocols.<br class="">Only some methods and properties might be abstract.<br class="">The goal of abstract classes is to encapsulate a generic behavior that may need some <br class="">specific implementation methods which are not known in abstract class. This behavior <br class="">requires attributes that are used by internal abstract class method.<br class=""><br class="">Example : <br class="">Considere a generic RESTClient that is included in a framework : <br class=""><br class="">```swift<br class="">class RESTClient {<br class=""><br class=""> &nbsp;&nbsp;var timeout = 3000<br class=""><br class=""> &nbsp;&nbsp;var url : String {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert(false,"Must be overriden")<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ""<br class=""> &nbsp;&nbsp;}<br class=""><br class=""> &nbsp;&nbsp;func performNetworkCall() {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let restURL = self.url<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Performing URL call to \(restURL) with timeout \(self.timeout)")<br class=""> &nbsp;&nbsp;}<br class="">}<br class=""><br class="">```<br class=""><br class="">And an implementation : <br class="">```swift<br class="">class MyRestServiceClient : RESTClient {<br class=""> &nbsp;&nbsp;override var url : String {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "<a href="http://www.foo.com/client" class="">http://www.foo.com/client</a>"<br class=""> &nbsp;&nbsp;}<br class=""><br class="">}<br class="">```<br class=""><br class="">As you can see, url properties must be implemented by inherited class and should not be <br class="">implemented by ancestor.<br class="">As workaround, we have added assertion, but this error is only detected at runtime and not <br class="">at compile time and might create crash for end-user.<br class=""><br class="">## Proposed solution<br class="">We propose to add a new keyword to indicate that a method or a property is abstract and <br class="">not implemented in current class.<br class="">This indicates that method or properties must be implemented in inherited class that can <br class="">be implemented.<br class="">We propose the keyword abstract that must be added to class and property/method : <br class=""><br class="">```swift<br class="">abstract class RESTClient { &nbsp;&nbsp;&nbsp;<br class=""> &nbsp;&nbsp;&nbsp;var timeout = 3000<br class=""><br class=""> &nbsp;&nbsp;abstract var url : String { get }<br class=""><br class=""> &nbsp;&nbsp;func performNetworkCall() {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let restURL = self.url<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Performing URL call to \(restURL) with timeout \(self.timeout)")<br class=""> &nbsp;&nbsp;}<br class="">}<br class="">```<br class=""><br class="">And an implementation : <br class="">```swift<br class="">class MyRestServiceClient : RESTClient {<br class=""> &nbsp;&nbsp;override var url : String {<br class=""> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "<a href="http://www.foo.com/client" class="">http://www.foo.com/client</a>"<br class=""> &nbsp;&nbsp;}<br class=""><br class="">}<br class="">```<br class=""><br class="">## Detailed design<br class="">An abstract class cannot be instanciated. <br class=""><br class="">If a class contains one or more abstract methods/properties, it must be declared abstract.<br class=""><br class="">A class that inherits from abstract must be declared abstract if it does not implements <br class="">all inherited methods/properties.<br class=""><br class="">If you try to implement an abstract class or a inherited class that implements partially <br class="">abstract methods/properties, you will get a compiler error.<br class=""><br class="">As for override keyword, abstract properties apply on setter, getter and observers. <br class=""><br class="">When declaring an abstract property, you must specify which methods must be implemented : <br class="">get, set, didSet, willSet. <br class=""><br class="">If you do not specify anything, only setter and getter are made <br class="">abstracts as below : <br class=""><br class="">```swift<br class=""> &nbsp;&nbsp;abstract var url : String<br class="">```<br class=""><br class="">Observers provides default empty implementation.<br class=""><br class="">Type is mandatory for abstract properties since it cannot be inferred.<br class=""><br class="">## Impact on existing code<br class="">This change has no impact on existing code, but might change the ABI that is being <br class="">stabilizing in Swift 3.0.<br class=""><br class="">## Alternatives considered<br class="">As first reading, it seems that protocols and protocol extensions might fit the need. It <br class="">actually does not because abstract classes can have attributs and properties that <br class="">protocols does not support.<br class=""><br class="">An alternative solution would be to add attributes to protocols and protocol extensions, <br class="">but this might break compatibility with Objective-C runtime.<br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); font-family: Verdana;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">David James</div></div></span></div></span></div></span></div></div>
</div>
<br class=""></div></body></html>