<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>I think an even stronger change should be made for #1: instead of explicitly opting into not discarding the default initializer, you should have to explicitly opt out of keeping it with something like @nosynthesizedinit. Unless I’m missing something and there is a compelling reason to remove the default initializer when you’ve provided another initializer.</div><div><br></div><div>I agree with #2 as is.</div><div><br></div><div>For #3, I imagine there’s some reason classes don’t get synthesized initializers. Probably because they’re meant to encapsulate a lot more functionality than merely maintaining state. Even if a class had an init with the “default” signature, it would almost certainly do more in the body than just assign them to instance variables. Since structs are so frequently used to simply hold onto state/keep data around, it makes sense for the compiler to provide an initializer that takes the entire initial state/set of data. In other words: if you find yourself using such an initializer for a class (an initializer that merely assigns the arguments to instance variables), you probably want to make that class a struct.</div><div><br>On Jun 22, 2017, at 9:38 AM, Bastiaan Marinus van de Weerd via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div>Strongly +1 on proposals #1 and #2. Cautiously +1 on #3 (I’m not sure about side effects there).</div><div><br></div><div>If #1 is already possible as Jon Shier says using an extension, I’d say it deserves promotion to be part of the language (e.g. Mike’s proposed `default init`), or at least should be made more discoverable (e.g. using a Fix-It on an implementation equivalent to the default initializer).</div><div><br></div><div><br></div><div>–Bastiaan</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 21, 2017 at 8:00 PM, Jon Shier via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>1 can already be accomplished by moving the custom initializer into an extension.&nbsp;</div><div><br></div><div><br></div><div>Jon</div><div><br><blockquote type="cite"><div><div class="h5"><div>On Jun 21, 2017, at 7:42 PM, Mike Kluev via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_2315186809508713651Apple-interchange-newline"></div></div><div><div><div class="h5"><div dir="ltr"><div>hi. here are a few somewhat related initializer proposals for your consideration.</div><div>they are designed as "opt-ins" so shall not break existing code.</div><div><br></div><div>***** proposal 1 *****</div><div>have an ability to keep default initializers in structs</div><div><br></div><div>struct S {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float</div><div>&nbsp; &nbsp; var bool: Bool</div><div>}</div><div><br></div><div>here default initializer is currently generated:</div><div><br></div><div>struct S {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float</div><div>&nbsp; &nbsp; var bool: Bool</div><div>&nbsp; &nbsp;&nbsp;</div><div>// &nbsp;autogenerated:</div><div>// &nbsp;init(int: Int, float: Float, bool: Bool) {</div><div>// &nbsp; &nbsp; &nbsp;<a href="http://self.int/" target="_blank">self.int</a> = int</div><div>// &nbsp; &nbsp; &nbsp;self.float = float</div><div>// &nbsp; &nbsp; &nbsp;self.bool = bool</div><div>// &nbsp;}</div><div>}</div><div><br></div><div>so we can write:</div><div><br></div><div>let s = S(int: 0, float: 0, bool: false)</div><div><br></div><div>so far so good. now i want to add a "convenience" initializer. i don't want the default initializer to go away though:</div><div><br></div><div>struct S {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float</div><div>&nbsp; &nbsp; var bool: Bool</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; /* convenience */ init(x: Int) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.init(int: 0, float: 0, bool: false) // *** ERROR. this initializer is gone</div><div>&nbsp; &nbsp; }</div><div>}</div><div><br></div><div>let s = S(x: 0)</div><div>let s = S(int: 0, float: 0, bool: false) // *** ERROR. this initializer is gone</div><div><br></div><div>this may be convenient in some cases but not others.</div><div>proposal: introduce an opt-in construct to keep the default initializer intact:</div><div><br></div><div>struct S {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float</div><div>&nbsp; &nbsp; var bool: Bool</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; default init // explicit opt-in. suggest a better name</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; /* convenience */ init(x: Int) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.init(int: 0, float: 0, bool: false) // still ok</div><div>&nbsp; &nbsp; }</div><div>}</div><div><br></div><div>let s = S(x: 0) // ok</div><div>let s = S(int: 0, float: 0, bool: false) // still ok</div><div><br></div><div>&nbsp; &nbsp;&nbsp;</div><div>***** proposal 2 *****</div><div>have the default initializer to have default nil values for all optional parameters in structs</div><div><br></div><div>struct S {<br></div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float?</div><div>&nbsp; &nbsp; var bool: Bool</div><div>&nbsp; &nbsp;&nbsp;</div><div>// &nbsp;autogenerated:</div><div>// &nbsp;init(int: Int, float: Float? = nil, bool: Bool) {</div><div>// &nbsp; &nbsp; &nbsp;<a href="http://self.int/" target="_blank">self.int</a> = int</div><div>// &nbsp; &nbsp; &nbsp;self.float = float</div><div>// &nbsp; &nbsp; &nbsp;self.bool = bool</div><div>// &nbsp;}</div><div>}</div><div><br></div><div>in the extreme case:</div><div><br></div><div>struct S5 {</div><div>&nbsp; &nbsp; var int: Int?</div><div>&nbsp; &nbsp; var float: Float?</div><div>&nbsp; &nbsp; var bool: Bool?</div><div>&nbsp; &nbsp;&nbsp;</div><div>// &nbsp;autogenerated:</div><div>// &nbsp;init(int: Int? = nil, float: Float? = nil, bool: Bool? = nil) {</div><div>// &nbsp; &nbsp; &nbsp;<a href="http://self.int/" target="_blank">self.int</a> = int</div><div>// &nbsp; &nbsp; &nbsp;self.float = float</div><div>// &nbsp; &nbsp; &nbsp;self.bool = bool</div><div>// &nbsp;}</div><div>}</div><div><br></div><div>usage:</div><div>S(float: 3)</div><div>S()</div><div>S(int: 0, float: 0, bool: false)</div><div><br></div><div>note that this is still "opt-in" as the old code will be using the full form anyway and not break.</div><div><br></div><div>***** proposal 3 *****</div><div>introduce a similar opt-in default initialiser for classes:</div><div><br></div><div>class C {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float?</div><div>&nbsp; &nbsp; var bool: Bool</div><div>}</div><div><br></div><div>let c = C(int: 0, float: 0, bool: false) // *** ERROR</div><div><br></div><div>class C {</div><div>&nbsp; &nbsp; var int: Int</div><div>&nbsp; &nbsp; var float: Float?</div><div>&nbsp; &nbsp; var bool: Bool</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; default init // explicit opt-in. suggest a better name</div><div>&nbsp; &nbsp;&nbsp;</div><div>// &nbsp;this is autogenerated:</div><div>// &nbsp;init(int: Int, float: Float? = nil, bool: Bool) {</div><div>// &nbsp; &nbsp; &nbsp;<a href="http://self.int/" target="_blank">self.int</a> = int</div><div>// &nbsp; &nbsp; &nbsp;self.float = float</div><div>// &nbsp; &nbsp; &nbsp;self.bool = bool</div><div>// &nbsp;}</div><div>}</div><div><br></div><div>let c = C(int: 0, bool: false) // ok</div><div><br></div><div><div><br></div><div>***** question 4 *****</div><div><br></div><div>this one is not a proposal, rather a question. probably there is a good reason for it, i just want to know it: why there is no default initializer for classes similar to what we have for structs?</div></div><div><br></div><div><br></div><div>==============================<wbr>===</div><div>thoughts and comments on any of these?</div><div><br></div><div>Mike</div><div><br></div></div></div></div>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div><br></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>