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

Evan Maloney emaloney at gilt.com
Thu Feb 25 09:06:55 CST 2016


>> • If a subclass implementor forgets to provide a retrieveText() function, the error will not be caught until runtime, and not until a user navigates to the affected portion of the application. This may not occur until the application has shipped.
> 
> That's one alternative, yes. Others include:
> 
> 1. Having a delegate provide the `retrieveText()` method.

I'm assuming the delegate will be a simple get/set property?

How can the compiler enforce that the developer supplies a delegate?

By declaring a function abstract, the compiler can enforce that the implementation is supplied. I don't see how you can do that with a delegate.

Using this solution, any bugs won't be caught until runtime.

> 2. Having a closure property implement the `retrieveText()` method.

My same objections above apply. You can make a closure property, but you can't guarantee that the developer supplies a value. So you end up with something like this:

var retrieveText: () -> String = {
    fatalError("You forgot to supply a retrieveText() closure!")
}

Again, this solution does not allow the compiler to enforce anything.

The beauty of the abstract class is that the compiler can catch these types of errors.

> 3. Declaring a protocol `ActivityViewControlling` that requires `retrieveText()` and adding the other logic in an `extension ActivityViewControlling where Self: UIViewController`.

Show me how to do that in a way that enforces the developer supply an implementation. I don't see how it can be done, and if it can, it's certainly going to be far more convoluted than an abstract class.


More information about the swift-evolution mailing list