I haven't seen a lot of discussion on the fragile base class problem w.r.t. Swift lately. After reading some discussion of the topic on the cocoa-dev forums, I decided to do a little test:
Framework code:
public class BaseClass {
public init() {}
public func foo() {
print("Foo called in framework class")
}
}App code:
import FBCFramework
class DerivedClass: BaseClass {
func bar() {
print("Bar called in subclass")
}
}
let obj = DerivedClass()
obj.foo()This returns what you'd expect:
Foo called in framework classNow, let's change the framework, so that it looks like this:
public class BaseClass {
public init() {}
public func foo() {
print("Foo called in framework class")
self.bar()
}
internal func bar() {
print("Bar called in framework class")
}
}Recompiling the framework, but not the app, and running results in:
Foo called in framework class
Bar called in subclassNow, let's try reordering the methods in the framework, so that it looks like this:
public class BaseClass {
public init() {}
internal func bar() {
print("Bar called in framework class")
}
public func foo() {
print("Foo called in framework class")
self.bar()
}
}Recompiling the framework and not the app results in this:
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
Foo called in framework class
...ad infinitum.
Recompiling the app, of course, fixes everything (until the next time the framework changes).
The conclusion seems to be that Swift, as of Xcode 7 beta 6, suffers from the fragile base-class problem even worse than the old Objective-C 1.0 runtime, since that was at least able to add/remove/reorder methods without causing problems with subclasses.
Are there plans to address this in the future?