<div>Would it be confusing if `guard var ` or  `if var ` <span></span>was only allowed for value types? </div><br><div><br>On Tuesday, January 26, 2016, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Tue Jan 26 2016, Tian Zhang &lt;<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;swift-evolution@swift.org&#39;)">swift-evolution@swift.org</a>&gt; wrote:<br>
<br>
&gt; I’m also curious how most people fix “guard var” or “if var”?<br>
&gt; Especially for checking a class object for protocol conformation and<br>
&gt; set variable on the object?<br>
&gt;<br>
&gt; like in this case,<br>
&gt;<br>
&gt;&gt; if var vc = vc as? ControlPanelConfigurationProtocol {<br>
&gt;&gt;         vc.servicePresentationObject = service<br>
&gt;&gt;         vc.presentAsPanel = true<br>
&gt;&gt; }<br>
&gt;<br>
&gt; become<br>
&gt;<br>
&gt;&gt; if let vc = vc as? ControlPanelConfigurationProtocol {<br>
&gt;&gt;         var vc = vc<br>
&gt;&gt;<br>
&gt;&gt;         vc.servicePresentationObject = service<br>
&gt;&gt;         vc.presentAsPanel = true<br>
&gt;&gt; }<br>
<br>
<br>
If vc has class type, you don&#39;t need the var at all.<br>
<br>
&gt; I saw a few people suggest to create a method on the protocol like<br>
&gt; “configureObject(...)” with all potential args and have the object to<br>
&gt; figure it out but doing so I feel we’re losing the benefits offered by<br>
&gt; property observation for the underlying object. Using pattern “if let”<br>
&gt; with a “var” in the block just to make the property mutable again<br>
&gt; feels really strange.<br>
&gt;<br>
&gt; Best Wishes,<br>
&gt; Tian<br>
&gt;&gt; An alternative would certainly be interesting but I would prefer to<br>
&gt;&gt; take it one step at a time and avoid being hasty so we can come up<br>
&gt;&gt; with something really great. What did most of your var fixes look<br>
&gt;&gt; like, by the way? Did you end up changing the layout of your value<br>
&gt;&gt; types or did you decide to add more vars?<br>
&gt;&gt;<br>
&gt;&gt; David<br>
&gt;&gt;<br>
&gt;&gt; &gt; On Jan 24, 2016, at 7:19 PM, Zach Waldowski via swift-evolution<br>
&gt;&gt; &gt; &lt;swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a><br>
&gt;&gt; &gt; &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;&gt; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; -1<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Having already adopted the syntax in my projects in anticipation of 2.2,<br>
&gt;&gt; &gt; the increase in clarity at the expense of terseness is appreciated. A<br>
&gt;&gt; &gt; proposal should not be discussing an alternative, not a rollback.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Cheers!<br>
&gt;&gt; &gt; Zachary Waldowski<br>
&gt;&gt; &gt; zach at <a href="http://waldowski.me" target="_blank">waldowski.me</a> &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Fri, Jan 22, 2016, at 12:26 PM, David Farler via swift-evolution<br>
&gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt; Hello everyone,<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I&#39;d like to reconsider SE-0003 for Swift 3 and propose cancelling the<br>
&gt;&gt; &gt;&gt; change in its entirety. After collecting feedback since Swift&#39;s open<br>
&gt;&gt; &gt;&gt; source launch, I no longer feel this is a good move and there are a few<br>
&gt;&gt; &gt;&gt; reasons why.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; There are two main patterns that the removal penalizes:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; - Get-Modify-Reassign<br>
&gt;&gt; &gt;&gt; - Get-Modify-Return<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I&#39;ve found that many of the problems with this proposal stem from the<br>
&gt;&gt; &gt;&gt; uses before and after the &quot;Modify&quot; part, before returning or reassigning<br>
&gt;&gt; &gt;&gt; with the new value.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I&#39;ve seen a few common responses to the var removal. Consider a<br>
&gt;&gt; &gt;&gt; `Rectangle` struct:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; struct Rectangle {<br>
&gt;&gt; &gt;&gt; var origin: (x: Double, y: Double)<br>
&gt;&gt; &gt;&gt; var size: (width: Double, height: Double)<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Even with mutable variables `origin` and `size`, this pattern would be<br>
&gt;&gt; &gt;&gt; impossible:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; var selection = getRectangularSelection()<br>
&gt;&gt; &gt;&gt; if var rect = selection?.rect {<br>
&gt;&gt; &gt;&gt; // Mutate `rect` ...<br>
&gt;&gt; &gt;&gt; selection.rect = rect<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; So, one might shadow the variable, which is not ideal:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; var selection = getRectangularSelection()<br>
&gt;&gt; &gt;&gt; if let rect = selection?.rect {<br>
&gt;&gt; &gt;&gt; var rect = rect // Not so great<br>
&gt;&gt; &gt;&gt; // Mutate `rect` ...<br>
&gt;&gt; &gt;&gt; selection.rect = rect<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Or, you might make a transformation function on `Rect`:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; struct Rectangle {<br>
&gt;&gt; &gt;&gt; var origin: (x: Double, y: Double)<br>
&gt;&gt; &gt;&gt; var size: (width: Double, height: Double)<br>
&gt;&gt; &gt;&gt; func withOrigin(x: Double, y: Double) -&gt; Rect {<br>
&gt;&gt; &gt;&gt;   var r = self<br>
&gt;&gt; &gt;&gt;   r.origin = (x, y)<br>
&gt;&gt; &gt;&gt;   return r<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; This is a much better solution than shadowing but you would need one of<br>
&gt;&gt; &gt;&gt; these for any property that you want to mutate and I think you&#39;ll agree<br>
&gt;&gt; &gt;&gt; that it doesn&#39;t scale with the language we have today. This response begs<br>
&gt;&gt; &gt;&gt; for a kind of initializer that takes all of the fields of the original<br>
&gt;&gt; &gt;&gt; struct except any that you want to override:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; if let rect = selection?.rect.with(origin: newOrigin) {<br>
&gt;&gt; &gt;&gt; // ...<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Straw syntax, but maybe you&#39;ll see something along these lines on<br>
&gt;&gt; &gt;&gt; swift-evolution in the future, which would provide a clear alternative to<br>
&gt;&gt; &gt;&gt; direct mutation patterns. Even then, I think having complementary<br>
&gt;&gt; &gt;&gt; patterns in the language isn&#39;t a bad thing.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; These problems come up with the other variable bindings but the one that<br>
&gt;&gt; &gt;&gt; ended up bothering me the most was `guard var`:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; func transform(selection: Rect?) {<br>
&gt;&gt; &gt;&gt; guard let rect = selection else { return }<br>
&gt;&gt; &gt;&gt; var _rect = rect<br>
&gt;&gt; &gt;&gt; // Mutate `_rect` ...<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; One of the guard statement&#39;s main purposes is to conditionally bind a<br>
&gt;&gt; &gt;&gt; value as a peer in its own scope, not an inner scope like if statements.<br>
&gt;&gt; &gt;&gt; Not having var makes the guard statement much weaker.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; There is certainly a bit of confusion about the nuances between value and<br>
&gt;&gt; &gt;&gt; reference semantics, who owns a value and when, how effects are<br>
&gt;&gt; &gt;&gt; propagated back to values, but I think we can attack the problem with<br>
&gt;&gt; &gt;&gt; more finesse.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Value types are one of the attractive features of Swift – because of<br>
&gt;&gt; &gt;&gt; their semantics, mutating algorithms are written in a familiar style but<br>
&gt;&gt; &gt;&gt; keeping effects limited to your unique reference. I don&#39;t think we should<br>
&gt;&gt; &gt;&gt; give that up now to address confusion about semantics, out of principle,<br>
&gt;&gt; &gt;&gt; or in anticipation of new language features. I propose cancelling this<br>
&gt;&gt; &gt;&gt; change for Swift 3 and continue to allow `var` in the grammar everywhere<br>
&gt;&gt; &gt;&gt; it occurs in Swift 2.2.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Regards,<br>
&gt;&gt; &gt;&gt; David<br>
&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; swift-evolution mailing list<br>
&gt;&gt; &gt;&gt; swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a> &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt; &gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;&gt; &gt;&gt; &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; swift-evolution mailing list<br>
&gt;&gt; &gt; swift-evolution at <a href="http://swift.org" target="_blank">swift.org</a> &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;&gt; &gt; &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;swift-evolution@swift.org&#39;)">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
--<br>
-Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;swift-evolution@swift.org&#39;)">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>
</blockquote></div>