<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 agree completely with Kevin. It doesn't seem intuitive to me that var in parameters and pattern matching is confusing. I actually see it the opposite way -- "why can I use var here but not there?". Allowing var wherever let can be used increases consistency, which is rarely a bad thing.</div><div class=""><br class=""></div><div class="">I checked for functions that use var parameters in one of my larger projects and found 29 cases (roughly half of which are implementations of arithmetic operators for custom types, which is an area that benefits significantly from this feature). Most of those cases are functions that return a mutated version of a parameter, which I find to be a common and useful pattern. Less common, but still valuable, are cases where it's convenient and logical to track some internal state in one of the function's parameters. For example, a function that draws a list of items of variable height starting at some Y position passed as a parameter can naturally calculate the Y position of the next item by adding each item's height to the initial Y value as it is rendered.</div><div class=""><br class=""></div><div class="">As someone mentioned earlier, I also think that this is a pretty unfortunate pattern:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">if</span>&nbsp;<span class="" style="color: rgb(187, 44, 162);">let</span>&nbsp;x = x {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="Apple-tab-span" style="white-space: pre;">        </span><span class="" style="color: rgb(187, 44, 162);">var</span>&nbsp;x = x</div></div><div class=""><br class=""></div><div class="">Now there are three x's -- an optional one, an immutable one, and a mutable one, all with the same name. That seems to me to create more possibility for confusion than if var does. You can give each one a different name, but I've found that more often than not, it's very difficult to come up with non-arbitrary distinct names in these kinds of situations, and using names like optionalX or mutableX is so off-putting as to be out of the question for me personally.</div><div class=""><br class=""></div><div class="">I agree that it's important to always weigh a feature's value vs. its complexity, but I believe this is a case where the value justifies the complexity.</div><div class=""><br class=""></div><div class="">Jarod</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 28, 2016, at 11:19, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Oh man, huge +1 from me here.<br class=""><br class="">Apparently I never actually read that proposal. I was under the impression it only removed var from function parameters. And while I find that mildly annoying, I was ok with it because of the argument for removing as many keywords as possible from function parameter lists (e.g. to open up those keywords to be used as external parameter names). Though I'd really prefer to keep var anyway.<br class=""><br class="">But I didn't realize until now that it also prohibits `var` from _all_ pattern matching, such as if-let, guard-let, etc. And I find that incredibly restrictive. I am not ok at all with that restriction. It makes no sense to me, and it makes a lot of very clean code become much messier by the addition of completely spurious `var foo = foo` lines everywhere. What's more, you can't even add lines like that everywhere because of Swift's prohibition from shadowing variables from the same scope (e.g. you can't shadow the variable bound from `guard let` because it's in the same scope).<br class=""><br class="">I see Dave Abrahams arguing that this feature is tripping some people up. But I don't see that as appropriate grounds for removing it from the language. If people don't understand `if var` or `for var x in` then they can just not write code that uses that feature. And if the argument is that people might have to read other code that uses that, well, there's plenty of stuff in Swift that you have to actually learn about before you can understand how it works, and using `var` in patterns does not seem like one of the trickiest things. And if the confusion stems from not understanding the difference between `var` and `inout`, I don't see how preventing someone from writing `if var` will solve that confusion, because the same person would presumably have the same confusion about `var foo = bar`.<br class=""><br class="">-Kevin Ballard<br class=""><br class="">On Fri, Jan 22, 2016, at 09:26 AM, David Farler via swift-evolution wrote:<br class=""><blockquote type="cite" class="">Hello everyone,<br class=""><br class="">I'd like to reconsider SE-0003 for Swift 3 and propose cancelling the change in its entirety. After collecting feedback since Swift's open source launch, I no longer feel this is a good move and there are a few 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 uses before and after the "Modify" part, before returning or reassigning with the new value.<br class=""><br class="">I've seen a few common responses to the var removal. Consider a `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 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) -&gt; Rect {<br class=""> &nbsp;&nbsp;var r = self<br class=""> &nbsp;&nbsp;r.origin = (x, y)<br class=""> &nbsp;&nbsp;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 these for any property that you want to mutate and I think you'll agree that it doesn't scale with the language we have today. This response begs for a kind of initializer that takes all of the fields of the original 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 swift-evolution in the future, which would provide a clear alternative to direct mutation patterns. Even then, I think having complementary 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 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 value as a peer in its own scope, not an inner scope like if statements. 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 reference semantics, who owns a value and when, how effects are propagated back to values, but I think we can attack the problem with more finesse.<br class=""><br class="">Value types are one of the attractive features of Swift – because of their semantics, mutating algorithms are written in a familiar style but keeping effects limited to your unique reference. I don't think we should give that up now to address confusion about semantics, out of principle, or in anticipation of new language features. I propose cancelling this change for Swift 3 and continue to allow `var` in the grammar everywhere it occurs in Swift 2.2.<br class=""><br class="">Regards,<br class="">David<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></body></html>