<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Thu, Jan 7, 2016, at 01:31 PM, Kevin Ballard wrote:<br></div>
<blockquote type="cite"><div>On Thu, Jan 7, 2016, at 07:12 AM, Matthew Johnson wrote:<br></div>
<blockquote type="cite"><div><blockquote type="cite"><div><div><div>As for my concern, it's with the following rule:<br></div>
<div>&nbsp;</div>
<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><div>&nbsp;</div>
<div>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></div>
</div>
</div>
</blockquote><div>&nbsp;</div>
<div>For normal initializers I agree with you. &nbsp;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. &nbsp;This warning is intended to alert you to the fact that you are violating that reasonable assumption.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>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.<br></div>
<div>&nbsp;</div>
<blockquote type="cite"><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>?<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>Sure, how about something like a Rect type that always guarantees it's in "standard" form (e.g. no negative sizes):<br></div>
<div>&nbsp;</div>
<div>struct StandardRect {<br></div>
<div>&nbsp; &nbsp; var origin: CGPoint<br></div>
<div>&nbsp; &nbsp; var size: CGSize {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; didSet {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ensure standardized form here<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }<br></div>
<div>&nbsp; &nbsp; }<br></div>
<div>&nbsp;</div>
<div>&nbsp; &nbsp; memberwise init(...) {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; if size.width &lt; 0 {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; origin.x += size.width<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size.width = -size.width<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; if size.height &lt; 0 {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; origin.y += size.height<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size.height = -size.height<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }<br></div>
<div>&nbsp; &nbsp; }<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>Or how about a struct that represents a URL request complete with headers, and forces the inclusion of Content-Type:<br></div>
<div>&nbsp;</div>
<div>struct URLRequest {<br></div>
<div>&nbsp; &nbsp; var headers: [String: String] = [:]<br></div>
<div>&nbsp; &nbsp; // ... other properties here ...<br></div>
<div>&nbsp; &nbsp; memberwise init(contentType: String, ...) {<br></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; headers["Content-Type"] = contentType<br></div>
<div>&nbsp; &nbsp; }<br></div>
<div>}<br></div>
</blockquote><div>&nbsp;</div>
<div>Here's an even better example, since it matches some code I actually already have in a production app. Assuming that the "..." placeholder can go anywhere (which seems reasonable), I have extensions on CGRect and friends that could use memberwise init instead to be written like<br></div>
<div>&nbsp;</div>
<div>extension CGRect {<br></div>
<div>&nbsp; &nbsp; memberwise init(..., roundedToScale: CGFloat)<br></div>
<div>}<br></div>
<div>&nbsp;</div>
<div>This method takes the components of a rect, as well as a scale, and it properly rounds the components so that the rect falls along pixel boundaries if displayed on a screen with the given scale.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
</body>
</html>