[swift-evolution] [Draft] Allow declaration of abstract functions and properties on classes

Vinicius Vendramini vinivendra at gmail.com
Fri Feb 26 15:30:02 CST 2016


I see, that seems about right. If we have a bas class that is not dumb, and especially if that base class needs to call abstract methods in its existing methods, I don’t think that solution would be enough.

I would still rather have a solution that extends protocols than one that relies specifically on classes, it seems to me that doing so would make the resulting functionality more generic (for instance, it might be possible to use it with structs as well as classes). However, I’m starting to agree that this is an important language feature indeed, since I can’t think of any better (or even equivalent) way of solving the example you showed.

In any case, I think the current proposal might be a bit too complex. Having to specify which property accessors are abstract (get, set, willSet or didSet), for instance, is going too far IMO. In its current form I do not support it (so -1 for me), but I could get behind the same idea using a simpler design.


> On Feb 26, 2016, at 2:20 PM, Vanderlei Martinelli <vmartinelli at alecrim.com> wrote:
> 
> Thank you. This is a good suggestion, but does not fit the case. The `AccountAuthorizationController` is an "abstract" class but it is not a "dumb" class. It store data, reference to injected class instances and do other important things. If I implement all of this as protocols I have to duplicate code here and there. I know I can use protocol extensions to provide default implementations. Even so, I'll have duplicate code.
> 
> Do not take me wrong, I really like protocols and structs and I understand how to use them, but it seems that many of us believe that the is something wrong with classes and the protocol/struct is the new Holy Grail. I disagree. New possibilites and new way to do things are always welcome, mainly if they are solid and consistent and really solves the problem intended to be solved.
> 
> I also understand that the Swift team does not want to introduce any new keywords to the language. To tell the truth I think I need to read again all the information on Swift. I thought the Swift would be one thing, but looks like it will be another. It is likely that the wrong one is me.
> 
> Provide ways to create abstract classes and abstract methods is not a "keystroke saver". It is one of many concepts involved in OOP, IMHO.
> 
> -Van
> 
> 
> 
> On Fri, Feb 26, 2016 at 3:45 PM, Vinicius Vendramini <vinivendra at gmail.com <mailto:vinivendra at gmail.com>> wrote:
> It seems like having a protocol restricted to instances of ‘AccountAuthorizationController’ (as Brent suggested) would solve your problem, wouldn’t it?
> 
> You could just add all method declarations that are supposed to be abstract to that protocol instead, and then make sure your subclasses implement the protocol - which means the compiler forces them to implement the abstract methods.
> 
> > Currently I have in a private framework a class named `AccountAuthorizationController` with subclasses like `OAuthAccountAuthorizationController`, `OAuth2AccountAuthorizationController` and `WebFormAccountAuthorizationController` and so on.
> > 
> > In the root "abstract" class I have methods without implementation where I have to use `fatalError()` to ensure that they will never been called. I cannot prevent the framework user to instantiate the `AccountAuthorizationController`, however.
> > 
> > Look that this is only one example. I have other cases as well when I'd like to have abstract classes and abstract methods.
> > 
> > I know that structs and protocols are "elegant, simple and powerful" (as they seem to be all new frameworks and languages that pop up every day on the Internet) and the argumentsin favor of composition rather than inheritance. But I still would like to take advantage of the decades of available knowledge in object orientation in my projects.
> > 
> > +1 for abstract classes and abstract methods.
> > 
> > -Van
> > 
> > On Fri, Feb 26, 2016 at 12:46 PM, Evan Maloney via swift-evolution<swift-evolution at swift.org <mailto:swift-evolution at swift.org>(mailto:swift-evolution at swift.org <mailto:swift-evolution at swift.org>)>wrote:
> > > >Well not exactly, if you want the same behaviors in subclasses of UIViewController and UITableViewController :
> > > >- with protocols + extensions, you write in once and apply it to each of your subclasses
> > > >- with abstract classes you have to write 2 abstract classes, one for direct UIViewController subclasses, one for UITableViewController subclasses
> > > 
> > > That's a problem with class hierarchies in general, not with abstract classes.
> > > 
> > > You can use the same argument to call for the removal of classes from Swift, which is why I think the fundamental question is, are classes intended to be first-class citizens in Swift?
> > > _______________________________________________
> > > swift-evolution mailing list
> > > swift-evolution at swift.org <mailto:swift-evolution at swift.org>(mailto:swift-evolution at swift.org <mailto:swift-evolution at swift.org>)
> > > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> > 
> > 
> > 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160226/10ae8cea/attachment.html>


More information about the swift-evolution mailing list