<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">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.<div class=""><br class=""></div><div class="">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?</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">I have no idea why super can be implicitly called and I see no reference to this in the language guide.</div><div class=""><br class=""></div><div class="">I am not sure I like this behavior. It should either be documented (maybe I am missing it?) or requiring calling super explicitly.</div><div class=""><br class=""></div><div class="">Brandon</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 16, 2016, at 10:09 AM, tuuranton--- via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" class="">
<div class="">
<div class="">Why does the following code compile?</div><div class="">Why does Bar's init(x: Int) automatically call Foo's init()?</div><div class="">Why don't I have to manually call super.init() myself?</div><div class=""><br class=""></div><div class="">What passage of</div><div class=""><a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203" target="_blank" class="">https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203</a></div><div class="">tells me that this should be the case?</div><div class=""><br class=""></div><div class="">--------------------</div><div class="">class Foo {</div><div class=""> init() {</div><div class=""> print("foo init")</div><div class=""> }</div><div class="">}</div><div class="">class Bar: Foo {</div><div class=""> init(x: Int) {</div><div class=""> print("bar init")</div><div class=""> }</div><div class="">}</div><div class="">let b = Bar(x: 0)</div><div class="">//prints:</div><div class="">// bar init</div><div class="">// foo init</div> </div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>