<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 18, 2017, at 3:54 PM, Chris McIntyre &lt;<a href="mailto:nothingwasdelivered@gmail.com" class="">nothingwasdelivered@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">Was it ever explained why the syntax is different?<br class=""><br class=""></div></div></div></blockquote><div><br class=""></div>Oh. I think convenience initializers on classes are marked as such explicitly because convenience vs designated has an effect on the ABI of an initializer, for the purposes of inheritance. With a value type, it doesn’t change anything about how the initializer is used externally.</div><div><br class=""></div><div>Yeah, probably the AST printer should not print the ‘convenience’ keyword on non-class initializers.</div><div><br class=""></div><div>Slava</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class="">--<div class="">Chris McIntyre</div></div><div class=""><br class="">On Jul 18, 2017, at 6:46 PM, Jens Persson via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class="">That is not true. Structs can have delegating initializers but they cannot be marked with `convenience` (only the initializers of class types can).</div><div class=""><br class=""></div><div class="">This is very clear from both the documentation and the compiler:</div><div class=""><br class=""></div><div class="">(1) The Swift Programming language (Swift 4):</div><div class=""><div class="">"Swift defines two kinds of initializers for *class* types to help ensure all stored properties receive an initial value. These are known as designated initializers and convenience initializers."</div><div class=""><br class=""></div></div><div class="">(2) The compiler:</div><div class=""><br class=""></div><div class="">struct S {</div><div class="">&nbsp; &nbsp; var a, b: Int</div><div class="">&nbsp; &nbsp; init(_ a: Int, _ b: Int) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; self.a = a</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; self.b = b</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; convenience init(_ ab: Int) { // &lt;-- Error</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; self.init(ab, ab)</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div><div class="">The error message is:<br class=""></div><div class="">Delegating initializers in structs are not marked with 'convenience'</div></div><div class="">The suggested fix is to remove the word convenience.</div><div class=""><br class=""></div>Please reread my previous post, perform the steps I describe (looking at Quick Help, also try jump to definition for that `init` and you'll see it is marked with `convenience` even though it is in a struct), also look at the link to the documentation for the Int init, it too is marked with convenience, even though Int is a struct.<div class=""><br class=""></div><div class="">/Jens</div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jul 18, 2017 at 9:46 PM, Slava Pestov <span dir="ltr" class="">&lt;<a href="mailto:spestov@apple.com" target="_blank" class="">spestov@apple.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Hi Jens,<div class=""><br class=""></div><div class="">While I’m not familiar with the integer API in the standard library, structs and enums certainly can have convenience initializers. They must delegate to another initializer (either convenience or designated) rather than initializing the fields of the type one by one.</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Jul 18, 2017, at 6:46 AM, Jens Persson via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="m_1803250266694613670Apple-interchange-newline"></div></div><div class=""><div class=""><div class="h5"><div dir="ltr" class=""><div class="">Start a command line project in Xcode 9 beta 3 and copy paste this single line of code into main.swift</div><div class=""><br class=""></div><div class=""><div class="">let _ = UInt8.init(<wbr class="">extendingOrTruncating: UInt64(123456))</div></div><div class=""><br class=""></div><div class="">Now look at Quick Help while placing the cursor on `init` and then on `extendingOrTruncating`.</div><div class=""><br class=""></div><div class="">Note that (and how) the documentation for the initializer differs depending on where you place the cursor.</div><div class=""><br class=""></div><div class="">If the cursor is on `init`, the initializer is shown to be a convenience(!) initializer even though structs (such as UInt8) cannot have convenience initializers, right?</div><div class=""><br class=""></div><div class="">Even the official documentation for this and several other initializer like eg:</div><div class=""><a href="https://developer.apple.com/documentation/swift/int/2885075-init" target="_blank" class="">https://developer.apple.com/<wbr class="">documentation/swift/int/<wbr class="">2885075-init</a></div><div class="">clearly shows convenience initializers in structs.</div><div class=""><br class=""></div><div class="">By the way, .init(extendingOrTruncating:) doesn't show in the list of completions for "UInt8.init" but it does for "UInt8(".</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Can anyone explain what's going on?</div><div class=""><br class=""></div><div class="">Are these known issues that will go away in time for Xcode 9 GM?</div><div class=""><br class=""></div><div class="">/Jens</div><div class=""><br class=""></div></div></div></div>
______________________________<wbr class="">_________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-users mailing list</span><br class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></body></html>