<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=""><div class="">I <i class="">still</i> don't understand how value types and reference types are different here. Here's a contrived "confusion" example (but we have seen similar real-world code):</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">for var element in array {</div><div class=""> if element.isNotUpToMyStandards {</div><div class=""> element = makeANewElement()</div><div class=""> // 'element' is not stored back into 'array', but was meant to be.</div><div class=""> }</div><div class="">}</div></blockquote><div class=""><br class=""></div><div class="">This code, or rather the behavior the developer intended, is perfectly reasonable. But it's exactly the same code whether there's a value type or a reference type involved. I get that there are other examples where this is <i class="">not</i> the case, but I can't see how <i class="">those</i> would be any <i class="">less</i> confusing by adding this rule.</div><div class=""><br class=""></div><div class="">But maybe I'm just too familiar with Swift, and so have trouble putting myself in the shoes of a new learner.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 27, 2016, at 20:45 , J. Cheyo Jimenez <<a href="mailto:cheyo@masters3d.com" class="">cheyo@masters3d.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">R<span style="font-size:12.8px" class="">eassigning of class objects probably occurs less than mutations of structs inside `if var` or `guard var`.</span><br class=""></div><div class=""><span style="font-size:12.8px" class="">The main reason for the removal of </span><span style="font-size:12.8px" class="">`if var` or `guard var`, AFAIK, is that people get confuse about references. Perhaps just limiting their use for value types could then clear up the confusion. </span></div><div class=""><span style="font-size:12.8px" class="">example of warning. </span></div><div class=""><span style="color:rgb(80,0,80);background-color:rgba(255,255,255,0)" class="">"'if var' is restricted to value types, did you mean 'if let'?"</span><br class=""></div><div class=""><font size="2" class=""><span style="background-color:rgba(255,255,255,0)" class=""><br class=""></span></font></div><div class="">One of the issues is that now all comma separated optionals need to be value types or offer alternative syntax. </div><div class=""><br class=""></div><div class="">if var value1 = value1, ref1 = ref1 {} /// This would not work</div><div class=""><br class=""></div><div class="">if var value1 = value1, let ref1 = ref1 {} /// Possible solution, notice the `let`<br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div class=""><br class=""></div><div class=""><br class=""></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Jan 27, 2016 at 8:05 PM, Jordan Rose <span dir="ltr" class=""><<a href="mailto:jordan_rose@apple.com" target="_blank" class="">jordan_rose@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">That doesn't make sense to me. If it's sometimes necessary to reassign a struct containing a single reference, then surely it may be necessary to reassign a single reference not contained in a struct.</div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Jordan</div></font></span><div class=""><div class="h5"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 26, 2016, at 20:37 , Nate Birkholz via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><div class=""><div dir="auto" class=""><div class="">Seems in line with other compiler warnings.<br class=""><br class="">Sent from my iPhone, please excuse brevity and errors</div><div class=""><br class="">On Jan 26, 2016, at 8:30 PM, J. Cheyo Jimenez via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="">Would it be confusing if `guard var ` or `if var ` <span class=""></span>was only allowed for value types? </div><br class=""><div class=""><br class="">On Tuesday, January 26, 2016, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
on Tue Jan 26 2016, Tian Zhang <<a class="">swift-evolution@swift.org</a>> wrote:<br class="">
<br class="">
> I’m also curious how most people fix “guard var” or “if var”?<br class="">
> Especially for checking a class object for protocol conformation and<br class="">
> set variable on the object?<br class="">
><br class="">
> like in this case,<br class="">
><br class="">
>> if var vc = vc as? ControlPanelConfigurationProtocol {<br class="">
>> vc.servicePresentationObject = service<br class="">
>> vc.presentAsPanel = true<br class="">
>> }<br class="">
><br class="">
> become<br class="">
><br class="">
>> if let vc = vc as? ControlPanelConfigurationProtocol {<br class="">
>> var vc = vc<br class="">
>><br class="">
>> vc.servicePresentationObject = service<br class="">
>> vc.presentAsPanel = true<br class="">
>> }<br class="">
<br class="">
<br class="">
If vc has class type, you don't need the var at all.<br class="">
<br class="">
> I saw a few people suggest to create a method on the protocol like<br class="">
> “configureObject(...)” with all potential args and have the object to<br class="">
> figure it out but doing so I feel we’re losing the benefits offered by<br class="">
> property observation for the underlying object. Using pattern “if let”<br class="">
> with a “var” in the block just to make the property mutable again<br class="">
> feels really strange.<br class="">
><br class="">
> Best Wishes,<br class="">
> Tian<br class="">
>> An alternative would certainly be interesting but I would prefer to<br class="">
>> take it one step at a time and avoid being hasty so we can come up<br class="">
>> with something really great. What did most of your var fixes look<br class="">
>> like, by the way? Did you end up changing the layout of your value<br class="">
>> types or did you decide to add more vars?<br class="">
>><br class="">
>> David<br class="">
>><br class="">
>> > On Jan 24, 2016, at 7:19 PM, Zach Waldowski via swift-evolution<br class="">
>> > <swift-evolution at <a href="http://swift.org/" target="_blank" class="">swift.org</a><br class="">
>> > <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>>> wrote:<br class="">
>> ><br class="">
>> > -1<br class="">
>> ><br class="">
>> > Having already adopted the syntax in my projects in anticipation of 2.2,<br class="">
>> > the increase in clarity at the expense of terseness is appreciated. A<br class="">
>> > proposal should not be discussing an alternative, not a rollback.<br class="">
>> ><br class="">
>> > Cheers!<br class="">
>> > Zachary Waldowski<br class="">
>> > zach at <a href="http://waldowski.me/" target="_blank" class="">waldowski.me</a> <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>><br class="">
>> ><br class="">
>> > On Fri, Jan 22, 2016, at 12:26 PM, David Farler via swift-evolution<br class="">
>> > wrote:<br class="">
>> >> Hello everyone,<br class="">
>> >><br class="">
>> >> I'd like to reconsider SE-0003 for Swift 3 and propose cancelling the<br class="">
>> >> change in its entirety. After collecting feedback since Swift's open<br class="">
>> >> source launch, I no longer feel this is a good move and there are a few<br class="">
>> >> reasons why.<br class="">
>> >><br class="">
>> >> There are two main patterns that the removal penalizes:<br class="">
>> >><br class="">
>> >> - Get-Modify-Reassign<br class="">
>> >> - Get-Modify-Return<br class="">
>> >><br class="">
>> >> I've found that many of the problems with this proposal stem from the<br class="">
>> >> uses before and after the "Modify" part, before returning or reassigning<br class="">
>> >> with the new value.<br class="">
>> >><br class="">
>> >> I've seen a few common responses to the var removal. Consider a<br class="">
>> >> `Rectangle` struct:<br class="">
>> >><br class="">
>> >><br class="">
>> >> struct Rectangle {<br class="">
>> >> var origin: (x: Double, y: Double)<br class="">
>> >> var size: (width: Double, height: Double)<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> Even with mutable variables `origin` and `size`, this pattern would be<br class="">
>> >> impossible:<br class="">
>> >><br class="">
>> >><br class="">
>> >> var selection = getRectangularSelection()<br class="">
>> >> if var rect = selection?.rect {<br class="">
>> >> // Mutate `rect` ...<br class="">
>> >> selection.rect = rect<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> So, one might shadow the variable, which is not ideal:<br class="">
>> >><br class="">
>> >><br class="">
>> >> var selection = getRectangularSelection()<br class="">
>> >> if let rect = selection?.rect {<br class="">
>> >> var rect = rect // Not so great<br class="">
>> >> // Mutate `rect` ...<br class="">
>> >> selection.rect = rect<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> Or, you might make a transformation function on `Rect`:<br class="">
>> >><br class="">
>> >><br class="">
>> >> struct Rectangle {<br class="">
>> >> var origin: (x: Double, y: Double)<br class="">
>> >> var size: (width: Double, height: Double)<br class="">
>> >> func withOrigin(x: Double, y: Double) -> Rect {<br class="">
>> >> var r = self<br class="">
>> >> r.origin = (x, y)<br class="">
>> >> return r<br class="">
>> >> }<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> This is a much better solution than shadowing but you would need one of<br class="">
>> >> these for any property that you want to mutate and I think you'll agree<br class="">
>> >> that it doesn't scale with the language we have today. This response begs<br class="">
>> >> for a kind of initializer that takes all of the fields of the original<br class="">
>> >> struct except any that you want to override:<br class="">
>> >><br class="">
>> >><br class="">
>> >> if let rect = selection?.rect.with(origin: newOrigin) {<br class="">
>> >> // ...<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> Straw syntax, but maybe you'll see something along these lines on<br class="">
>> >> swift-evolution in the future, which would provide a clear alternative to<br class="">
>> >> direct mutation patterns. Even then, I think having complementary<br class="">
>> >> patterns in the language isn't a bad thing.<br class="">
>> >><br class="">
>> >> These problems come up with the other variable bindings but the one that<br class="">
>> >> ended up bothering me the most was `guard var`:<br class="">
>> >><br class="">
>> >><br class="">
>> >> func transform(selection: Rect?) {<br class="">
>> >> guard let rect = selection else { return }<br class="">
>> >> var _rect = rect<br class="">
>> >> // Mutate `_rect` ...<br class="">
>> >> }<br class="">
>> >><br class="">
>> >><br class="">
>> >> One of the guard statement's main purposes is to conditionally bind a<br class="">
>> >> value as a peer in its own scope, not an inner scope like if statements.<br class="">
>> >> Not having var makes the guard statement much weaker.<br class="">
>> >><br class="">
>> >> There is certainly a bit of confusion about the nuances between value and<br class="">
>> >> reference semantics, who owns a value and when, how effects are<br class="">
>> >> propagated back to values, but I think we can attack the problem with<br class="">
>> >> more finesse.<br class="">
>> >><br class="">
>> >> Value types are one of the attractive features of Swift – because of<br class="">
>> >> their semantics, mutating algorithms are written in a familiar style but<br class="">
>> >> keeping effects limited to your unique reference. I don't think we should<br class="">
>> >> give that up now to address confusion about semantics, out of principle,<br class="">
>> >> or in anticipation of new language features. I propose cancelling this<br class="">
>> >> change for Swift 3 and continue to allow `var` in the grammar everywhere<br class="">
>> >> it occurs in Swift 2.2.<br class="">
>> >><br class="">
>> >> Regards,<br class="">
>> >> David<br class="">
>> >> _______________________________________________<br class="">
>> >> swift-evolution mailing list<br class="">
>> >> swift-evolution at <a href="http://swift.org/" target="_blank" class="">swift.org</a> <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>><br class="">
>> >> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
>> >> <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>><br class="">
>> > _______________________________________________<br class="">
>> > swift-evolution mailing list<br class="">
>> > swift-evolution at <a href="http://swift.org/" target="_blank" class="">swift.org</a> <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>><br class="">
>> > <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
>> > <<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>><br class="">
> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
--<br class="">
-Dave<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>