<!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> </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> </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> </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.<br></div>
</div>
</blockquote><div> </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> </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> </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> </div>
<div>struct StandardRect {<br></div>
<div> var origin: CGPoint<br></div>
<div> var size: CGSize {<br></div>
<div> didSet {<br></div>
<div> // ensure standardized form here<br></div>
<div> }<br></div>
<div> }<br></div>
<div> </div>
<div> memberwise init(...) {<br></div>
<div> if size.width < 0 {<br></div>
<div> origin.x += size.width<br></div>
<div> size.width = -size.width<br></div>
<div> }<br></div>
<div> if size.height < 0 {<br></div>
<div> origin.y += size.height<br></div>
<div> size.height = -size.height<br></div>
<div> }<br></div>
<div> }<br></div>
<div>}<br></div>
<div> </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> </div>
<div>struct URLRequest {<br></div>
<div> var headers: [String: String] = [:]<br></div>
<div> // ... other properties here ...<br></div>
<div> memberwise init(contentType: String, ...) {<br></div>
<div> headers["Content-Type"] = contentType<br></div>
<div> }<br></div>
<div>}<br></div>
</blockquote><div> </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> </div>
<div>extension CGRect {<br></div>
<div> memberwise init(..., roundedToScale: CGFloat)<br></div>
<div>}<br></div>
<div> </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> </div>
<div>-Kevin Ballard</div>
<div> </div>
</body>
</html>