Swift: Required init should generate warnings not errors
| Originator: | freerunnering | ||
| Number: | rdar://19346576 | Date Originated: | 25-Dec-2014 08:40 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode Version 6.1.1 & OSX 10.10 |
| Classification: | Bug | Reproducible: | Always |
Summary:
The concept of a "Required init" is silly. Being able to subclass something means your class inherits its methods.
If a method is not implemented than it simply falls through to the superclasses implementation.
In Swift though init methods can be marked "required".
There are a number of possible uses to this, making sure variables are initialised and things aren't missed.
However, missing these methods should ONLY generate compiler warnings! And not errors!!
The concept of a subclass means calling that method, if it does not exist falls though to the superclass.
The "required init" should be advisory, and so should generate a warning.
An example of this is "init(coder)" on UIViewController subclasses.
It is required that subclasses include this if they introduce any init methods of their own.
As a result the subclass all have this 1 line of code in them:
required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
This accomplishes nothing! This is simply making developers right the superclass forwarding manually!
This goes against automatically inheriting methods behaviour that subclasses have.
The point of required init methods is to advice what you need to take note of. That is the job of a compiler "WARNING".
Compiler errors should ONLY be for invalid code that will not compile.
NOT code that goes against best practises or code that will generate correct code.
The code is 100% perfect without the "required" method their. If you could override that 'error' the compiled code would execute successfully. This can even be tested by writing the same class in ObjC and not adding an -initWithCoder. The code will work perfectly, simply calling its superclasses method instead.
This should be a compiler "WARNING" not an "Error". Not only does having missing "Required init" methods generate "Errors" violate the concept of inheritance, it is also a violation of the concept of "compiler error" and a "compiler warning".
Steps to Reproduce:
1: Try to write a UIViewController subclass.
2: Add an
init(text: NSString) {
super.init()
}
...method.
3: Compile
Expected Results:
The code will compile and run, with the possibility of a compiler warning about an "init(coder aDecoder: NSCoder)" method having not been implemented and a warning about the "text" variable being unused.
Actual Results:
The compiler generates an "error", because I am required to write...
required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
...manually, instead of letting inheritance do that automatically for me.
Version:
Xcode Version 6.1.1 & OSX 10.10
Notes:
Compiler Errors should be reserved only for 'errors' the compiled had turning your code into an executable.
Artificial warnings, about code style or other things should only ever generate "warnings"!
A compiler error should never something which, if bypassed, the compiler would generate a valid executable from valid code.
Configuration:
Attachments:
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!