[swift-users] super.init() called automatically?
Brandon Knope
bknope at me.com
Mon May 16 10:10:38 CDT 2016
I did not know of this behavior, but it looks like if the superclasses designated initializer is just a plain init() then it will automatically call super.init() from your subclass.
There is no way to avoid not calling your superclasses’s designated initializer and I guess the compiler ensures this by implicitly placing a call to super?
For example, if you change your Foo init to init(test:Int) { } then your subclass will complain that it’s not calling super’s designated initializer.
I have no idea why super can be implicitly called and I see no reference to this in the language guide.
I am not sure I like this behavior. It should either be documented (maybe I am missing it?) or requiring calling super explicitly.
Brandon
> On May 16, 2016, at 10:09 AM, tuuranton--- via swift-users <swift-users at swift.org> wrote:
>
> Why does the following code compile?
> Why does Bar's init(x: Int) automatically call Foo's init()?
> Why don't I have to manually call super.init() myself?
>
> What passage of
> https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203 <https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203>
> tells me that this should be the case?
>
> --------------------
> class Foo {
> init() {
> print("foo init")
> }
> }
> class Bar: Foo {
> init(x: Int) {
> print("bar init")
> }
> }
> let b = Bar(x: 0)
> //prints:
> // bar init
> // foo init
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160516/1e5d7835/attachment.html>
More information about the swift-users
mailing list