<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Fri, Jan 8, 2016, at 12:56 AM, Thorsten Seitz wrote:<br></div>
<blockquote type="cite"><div>&nbsp;</div>
<div><blockquote type="cite"><div>Am 08.01.2016 um 00:41 schrieb Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;:<br></div>
<div>&nbsp;</div>
<div><div><div>On Thu, Jan 7, 2016, at 03:11 PM, Matthew Johnson wrote:<br></div>
<blockquote type="cite"><div>&nbsp;</div>
<div><blockquote type="cite"><div>On Jan 7, 2016, at 3:31 PM, Kevin Ballard &lt;<a href="mailto:kevin@sb.org">kevin@sb.org</a>&gt; wrote:<br></div>
<div>&nbsp;</div>
<div><div><div>On Thu, Jan 7, 2016, at 07:12 AM, Matthew Johnson wrote:<br></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>
</div>
</blockquote><div>&nbsp;</div>
<div>This is a good example. &nbsp;Thanks! &nbsp;<br></div>
</div>
</blockquote></div>
</div>
</blockquote><div>&nbsp;</div>
<div>Actually I do not like this example for several reasons: (1) I would make the rectangle an immutable type with let properties, (2) the didSet already seems to do what is encoded in the memberwise init, so this seems to be redundant, (3) the memberwise init is so complex that having the automatic initialization feature is not really worth it for this example, especially as it seems to require using var properties instead of let properties to do the overwriting.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>1) Why would you make it immutable? That helps nothing and only serves to make the type harder to use. Structs should _rarely_ be immutable, you should always default to mutable and only make things immutable if there's a good reason for it. If the struct itself is in an immutable position then it inherits the immutability, which handles all of the reasons why you might otherwise default to immutable.<br></div>
<div>&nbsp;</div>
<div>2) didSet isn't triggered in init. There's no redundancy.<br></div>
<div>&nbsp;</div>
<div>3) You really really want var properties anyway, it's pointless to use let properties.<br></div>
<div>&nbsp;</div>
<blockquote type="cite"><div><blockquote type="cite"><div><blockquote type="cite"><div><div>I think cases like this will be rare so I still think a warning is a good idea. &nbsp;Something like -Wno-overwrite-memberwise-init would allow it to be suppressed in cases where you actually do intend to do this. &nbsp;Would that satisfy you?<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>No. It's not appropriate to have the only way to suppress a warning on perfectly legal code to be passing a flag to the swiftc invocation. Especially because we have no precedent yet for even having flags like that.<br></div>
<div>&nbsp;</div>
<div>What's wrong with the suggestion to make the warning behave the same way as dead store warnings (e.g. warn if the property is overwritten without any prior reads)? We already have logic for doing this kind of analysis.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>&nbsp;</div>
<div>I think this would not be sufficient, because this would not allow overwriting a property based on the value of another property which might be necessary as well.<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>That seems much less likely to be necessary, because if you're doing that, then you're completely ignoring one of your parameters.</div>
<div>&nbsp;</div>
<blockquote type="cite"><div><div>Actually isn’t this what happens in your example? The property origin is overwritten without being read, so this would generate the warning, or did I understand something wrong?<br></div>
</div>
</blockquote><div>&nbsp;</div>
<div>Origin is being modified. Modification reads it first. `x += 2` reads `x` before writing to it.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
</body>
</html>