The following code compiles, but fails at runtime. I tried in an XCode playground and REPL.
The Child class overrides all of the Parent class designated initializers, and therefore it automatically inherits all of the superclass convenience initializers. But the Parent convenience initializer reasonably delegates to it’s designated initializer, which has been overridden by the subclass. So it is not the Parent version of that 2-argument initializer that is invoked, but the overridden version. The overridden version takes advantage of the fact that the subclass has inherited Parent’s convenience initializer, and there it is - you have a cycle. I don’t know if this would be considered programmer-mistake or a compiler bug.
I'm using Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81).
classParent {
varparentProperty1:StringvarparentProperty2:Stringinit(parentProperty1:String, parentProperty2:String) {
self.parentProperty1 = parentProperty1self.parentProperty2 = parentProperty2
}
convenienceinit(parentProperty1:String) {
print("in the Parent convenience initializer")
self.init(parentProperty1:parentProperty1, parentProperty2:"nada")
}
}
classChild : Parent {
varchildProperty:Doubleinit(parentProperty1: String, parentProperty2:String, childProperty:Double) {
self.childProperty = childPropertysuper.init(parentProperty1: parentProperty1, parentProperty2:parentProperty2)
}
// It seems that this trips up the compiler?convenienceoverrideinit(parentProperty1:String, parentProperty2:String) {
//this invokes the inherited convenience initializer, which invokes this overidden//version of the parent's designated initializer, which invokes the inherited convenience initializer,// cycle repeats until crash.print("in the subclass overridden convenience-version of the parent designated initializer")
self.init(parentProperty1:parentProperty1)
}
}
letchild = Child(parentProperty1: "This is", parentProperty2: "the central scrutinizer")
print("\(child.parentProperty1)...\(child.parentProperty2). childProperty: \(child.childProperty)")
The text was updated successfully, but these errors were encountered:
Attachment: Download
Additional Detail from JIRA
md5: eb715e88ccf3f5c8c3fc3862a39f6556
Issue Description:
The following code compiles, but fails at runtime. I tried in an XCode playground and REPL.
The Child class overrides all of the Parent class designated initializers, and therefore it automatically inherits all of the superclass convenience initializers. But the Parent convenience initializer reasonably delegates to it’s designated initializer, which has been overridden by the subclass. So it is not the Parent version of that 2-argument initializer that is invoked, but the overridden version. The overridden version takes advantage of the fact that the subclass has inherited Parent’s convenience initializer, and there it is - you have a cycle. I don’t know if this would be considered programmer-mistake or a compiler bug.
I'm using Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81).
The text was updated successfully, but these errors were encountered: