<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 Jan 7, 2016, at 3:31 PM, Kevin Ballard <<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class=""><div class="">On Thu, Jan 7, 2016, at 07:12 AM, Matthew Johnson wrote:<br class=""></div>
<blockquote type="cite" class=""><div class=""><blockquote type="cite" class=""><div class=""><div class=""><div class="">As for my concern, it's with the following rule:<br class=""></div>
<div class=""> </div>
<blockquote type="cite" class="">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 class=""></blockquote><div class=""> </div>
<div class="">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's been assigned to.<br class=""></div>
</div>
</div>
</blockquote><div class=""> </div>
<div class="">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.<br class=""></div>
</div>
</blockquote><div class=""> </div>
<div class="">I think that's a reasonable assumption in many cases, but I don't like the fact that the feature cannot be used at all in the rare case where it actually makes sense to mutate the value.</div>
<div class=""> </div>
<blockquote type="cite" class=""><div class=""><div class="">Do you have an example of where you would want a caller to initialize a property, but then overwrite the value they provide <b class="">during initialization</b>?<br class=""></div>
</div>
</blockquote><div class=""> </div>
<div class="">Sure, how about something like a Rect type that always guarantees it's in "standard" form (e.g. no negative sizes):<br class=""></div>
<div class=""> </div>
<div class="">struct StandardRect {<br class=""></div>
<div class=""> var origin: CGPoint<br class=""></div>
<div class=""> var size: CGSize {<br class=""></div>
<div class=""> didSet {<br class=""></div>
<div class=""> // ensure standardized form here<br class=""></div>
<div class=""> }<br class=""></div>
<div class=""> }<br class=""></div>
<div class=""> </div>
<div class=""> memberwise init(...) {<br class=""></div>
<div class=""> if size.width < 0 {<br class=""></div>
<div class=""> origin.x += size.width<br class=""></div>
<div class=""> size.width = -size.width<br class=""></div>
<div class=""> }<br class=""></div>
<div class=""> if size.height < 0 {<br class=""></div>
<div class=""> origin.y += size.height<br class=""></div>
<div class=""> size.height = -size.height<br class=""></div>
<div class=""> }<br class=""></div>
<div class=""> }<br class=""></div>
<div class="">}<br class=""></div></div></div></blockquote><div><br class=""></div><div>This is a good example. Thanks! </div><div><br class=""></div><div>I think cases like this will be rare so I still think a warning is a good idea. Something like -Wno-overwrite-memberwise-init would allow it to be suppressed in cases where you actually do intend to do this. Would that satisfy you?</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">
<div class=""> </div>
<div class="">Or how about a struct that represents a URL request complete with headers, and forces the inclusion of Content-Type:<br class=""></div>
<div class=""> </div>
<div class="">struct URLRequest {<br class=""></div>
<div class=""> var headers: [String: String] = [:]<br class=""></div>
<div class=""> // ... other properties here ...<br class=""></div>
<div class=""> memberwise init(contentType: String, ...) {<br class=""></div>
<div class=""> headers["Content-Type"] = contentType<br class=""></div>
<div class=""> }<br class=""></div>
<div class="">}</div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div class="">
<div class=""> </div>
<div class="">-Kevin Ballard</div>
</div>
</div></blockquote></div><br class=""></body></html>