[Swift] Runtime crash with `self.dynamicType()` if superclass does not have `required init() {}` method

Originator:wirth_caesar
Number:rdar://19322619 Date Originated:22-Dec-2014
Status:Closed (Resolved) Resolved:
Product:Developer Tools Product Version:Xcode Version 6.2 (6C86e)
Classification:Enhancement/Bug Reproducible:Always
 
In order to create an object of the same type by using `self.dynamicType()`, an object must implement `override required init()`. In most cases, this works OK, but there are some cases in which it doesn't. 

In particular, when a subclass does implement the required init, but the superclass either does not implement it, or it is not marked as required.

For example, the following code will die with EXC_BAD_ACCESS on line 11: 

1 class SuperClass {
2 
3 }
4 
5 class ChildClass: SuperClass {
6     required override init() {
7         super.init()
8     }
9     
10     func makeCopy() -> Self {
11        return self.dynamicType()
12     }
13   
14     func toString() -> String {
15         return "Child Class"
16     }
17 }
18 
19 let childObject = ChildClass()
20 let copyChild = childObject.makeCopy()
21 println(copyChild.toString())

but will succeed if you add
`required init() {}`
to line 2 and remove `override` from line 6

I understand that perhaps this could be considered user error, but it also seems like something that maybe the compiler could notice, and throw out an error or warning. Maybe even just having a better error message than exc_bad_error. That way it would be easier to debug when these problems arise in code where it is not so easy to notice what the cause is.

Version:
Xcode Version 6.2 (6C86e)
iOS 8 Simulator (iPhone 6 Simulator)
Mac OS X 10.9.5

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!