[swift-evolution] Making protocol conformance inheritance controllable

Gwendal Roué gwendal.roue at gmail.com
Fri Dec 11 04:53:24 CST 2015

Been bitten also.

> Le 11 déc. 2015 à 04:03, Matthew Johnson via swift-evolution <swift-evolution at swift.org> a écrit :
> I have run into this issue myself.  It is definitely a problem and we need a solution for it.  It is exacerbated by the fact that many Cocoa types which would naturally be value types or final classes in Swift are not designed this way due to their Objective-C heritage.

Foundation also exposes many non-final classes such as the immutable/mutable couples NSArray/NSMutableArray etc.

> Le 11 déc. 2015 à 03:04, Joe Groff via swift-evolution <swift-evolution at swift.org> a écrit :

> initializer requirements must be satisfied by `required` initializers (which then must be overridden in all derived classes, to the pain of anyone touching an NSCoding-inherited class), and methods often must return dynamic `Self` when they'd really prefer to return the base class.

This is especially painful when you want to extend existing non-final classes, such as Foundation classes.

***Fortunately***, those often provide the needed constructors (including copy constructors), which alleviates the fact that one can not add a required initializer to an extension to a non-final class.

    protocol P {
        static func f() -> Self?

    extension NSString : P {
        class func f() -> Self? {
            return self.init(string: "foo")

But the presence of those constructors in Foundation looks like a matter of luck.

Gwendal Roué

More information about the swift-evolution mailing list