<div dir="ltr">Overall I think this is a definite improvement to the current status quo and something needs to be solved. My only concern with this specific proposal is that on an initial glance it&#39;s not easy to intuit what the behavior is. I like the end result of this proposal, and I think since it&#39;d be frequently used it&#39;d be easy to remember what it was despite being unintuitive.<div><br></div><div>I had originally suggested this alternative:</div><div>```</div><div><div>struct Foo {</div><div>    let bar: String</div><div>    let bas: Int</div><div>    let baz: Double</div><div>    init(self.bar: String, self.bas: Int, bax: Int) {</div><div>          // self.bar = bar synthesized by the compiler</div><div>          // self.bas = bas synthesized by the compiler</div><div>        self.baz = Double(bax)</div><div>    }</div><div>}</div></div><div>```</div><div>The upside of this is that it&#39;s more clear what happens, downside is that adding a property requires 2 changes (as opposed to current 3, but more than the 1 change required in proposal).</div><div><br></div><div>Perhaps a syntax more along the lines of this would be more intuitive and still eliminate the boilerplate.</div><div>```</div><div><div>struct Foo {</div><div>    let bar: String</div><div>    let bas: Int</div><div>    let baz: Double</div><div>    init(bax: Int, self = ...) {</div><div>          // self.bar = bar synthesized by the compiler</div><div>          // self.bas = bas synthesized by the compiler</div><div>        self.baz = Double(bax)</div><div>    }</div><div>}</div></div><div>```</div><div>`memberwise` means nothing to the uninitiated, and `...` without any other operator is a little confusing. I think this solves the issue of saying what the `...`  is doing.</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jan 7, 2016 at 10:12 AM Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Jan 7, 2016, at 2:46 AM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div><blockquote type="cite"><span style="white-space:pre-wrap">        </span>* What is your evaluation of the proposal?<br></blockquote><br>+1 to the basic proposal. I&#39;m much more reserved about the &quot;Future enhancements&quot;; some I don&#39;t want at all, some sound plausible but probably need changes.<br></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Thanks for your support Kevin!  </div><div><br></div><div>As I stated last night, there are two primary things that I think need improving via enhancements of some kind.  I hope you can agree with these points:</div><div><br></div><div></div></div></div><div style="word-wrap:break-word"><div><div><div>1. We need a way to specify a default value for memberwise parameters for `let` properties.</div></div></div></div><div style="word-wrap:break-word"><div><div><div>2. We need a little bit more control over which properties participate in memberwise initialization when the “automatic” rules don’t quite do the right thing for a particular use case.</div><div><br></div><div>Most of the enhancements listed show various ways we could address the second point.  We don’t need all of them.  </div><div><br></div><div>Under the current proposal there will be cases where memberwise initialization would be of great use, but the rules of the automatic model expose a property (probably a `let`) that shouldn’t be exposed.  We won’t be able to use memberwise initialization at all in those cases if we don’t have a way to correct that behavior.</div><div><br></div><div>There may also be cases where a property doesn’t participate in memberwsie initialization when we would like it to.  In those cases we can add an explicit parameter and manually initialize the property, continuing to use memberwise initialization for other properties.</div><div><br></div><div>Ideally we can find a simple enhancement that solves both visibility problems.  I’m definitely open to any ideas on how we can handle this.</div></div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div><br>Also, a question and a concern about the basic proposal. The question: you state that the only impact this has on existing code is structs with private properties that have been getting an implicit internal memberwise initializer will have the initializer be private. That&#39;s fine, but assuming that the implicit memberwise initializer behaves identically to `memberwise init(...) {}`, surely this proposal also makes the implicit memberwise initializer gain defaulted arguments for any var properties with an initializer expression? Don&#39;t get me wrong, I think it&#39;s good to change that, but it should be explicitly noted.<br></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>That is a great point!  It won’t break any existing code, but it will change behavior slightly.  I will update the proposal and submit a PR with this change.</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div><br>As for my concern, it&#39;s with the following rule:<br><br><blockquote type="cite">If the initializer body assigns to a var property that received memberwise initialization synthesis report a warning. It is unlikely that overwriting the value provided by the caller is the desired behavior.<br></blockquote><br>I understand why you put this in there, but this is a warning that cannot be suppressed and will make it impossible to use a memberwise initializer for perfectly legitimate cases where you do in fact want to mutate the property after it&#39;s been assigned to.<br></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>For normal initializers I agree with you.  However, I think it’s a reasonable for callers to assume that if you expose a property via memberwise initialization the post-initialization value will match the value they provide.  This warning is intended to alert you to the fact that you are violating that reasonable assumption.</div><div><br></div><div>Do you have an example of where you would want a caller to initialize a property, but then overwrite the value they provide <b>during initialization</b>?</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div><br>What might be a reasonable thing is a warning that occurs if you assign to the var property without ever having read from it (i.e. a dead store warning on the memberwise initialization of the property). That way if I mutate a property to contain a derived value it&#39;s fine, but if I simply write to it without ever reading it, it&#39;s a problem.</div></div></blockquote><blockquote type="cite"><div><div><br><blockquote type="cite"><span style="white-space:pre-wrap">        </span>* Is the problem being addressed significant enough to warrant a change to Swift?<br></blockquote><br>I think so. Writing initializers can be a large source of boilerplate in Swift, especially when using classes where you can&#39;t get the implicit memberwise initializer.<br><br><blockquote type="cite"><span style="white-space:pre-wrap">        </span>* Does this proposal fit well with the feel and direction of Swift?<br></blockquote><br>Yes.<br><br><blockquote type="cite"><span style="white-space:pre-wrap">        </span>* If you have you used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?<br></blockquote><br>I&#39;m not aware of a similar feature in any language I&#39;m familiar with.<br><br><blockquote type="cite"><span style="white-space:pre-wrap">        </span>* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?<br></blockquote><br>I spent a few minutes reading over the whole proposal. I did not read any of the swift-evolution thread.<br><br>-Kevin Ballard<br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br></div></div></blockquote></div></div><div style="word-wrap:break-word"><div></div><br>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RVJ3oK8DhV2nk8GArr3gdPix5kxIdH2eteeGb4fQAElgi4B1fqRMIRCn67zieEoZ1ufAr2URxXF8LGi3VDNv3-2FdxB-2FtQJRAT-2BXAgZwK06WpZGLyv4er-2FPzk1lEH2qdMaaXoiMKMJdO2TRWpKYg3V4NzgJ7alCxS00E-2Bme2lf5SccnZOS27H93cQVgusj0fyP0HRNJA7eREmsyarYn9TCjg-3D-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>