<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 21, 2016, at 4:34 AM, Jeremy Pereira &lt;<a href="mailto:jeremy.j.pereira@googlemail.com" class="">jeremy.j.pereira@googlemail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><br class="Apple-interchange-newline">On 20 Dec 2016, at 13:10, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:<br class=""><br class=""><br class=""><br class="">Sent from my iPad<br class=""><br class=""><blockquote type="cite" class="">On Dec 20, 2016, at 4:32 AM, Jeremy Pereira via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On 20 Dec 2016, at 07:54, Pierre Monod-Broca via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">But for a struct to be immutable, you don't need all its properties to be let. (I guess that's Derrick's point)<br class=""></blockquote><br class="">Yes you do. Consider<br class=""><br class="">struct Person: Hashable<br class="">{<br class="">&nbsp;let firstName: String<br class="">&nbsp;let lastName: String<br class="">&nbsp;let hashValue: Int<br class=""><br class="">&nbsp;init(firstName: String, lastName: String)<br class="">&nbsp;{<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.firstName = firstName<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.lastName = lastName<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.hashValue = firstName.hashValue ^ lastName.hashValue<br class="">&nbsp;}<br class="">}<br class=""><br class="">func == (l: Person, r: Person) -&gt; Bool<br class="">{<br class="">&nbsp;return l.firstName == r.firstName &amp;&amp; l.lastName == r.lastName<br class="">}<br class=""><br class="">Pretend that the hash value is quite expensive to calculate so I only want to do it once. With the above code, this is fine but if I change the lets to vars (e.g. var firstName: String), I am open to<br class=""><br class="">let chris = Person(firstName: “Chris”, lastName: “Lattner”) // Immutable<br class=""><br class="">var andy = chris<br class="">andy.firstName = “Andy”<br class=""><br class="">andy.hashValue // Gives the wrong answer unless you are exceptionally lucky.<br class=""><br class="">I’ve used hashValue, but the same argument would apply to any computed property where you might want to cache the computed value. As soon as you make any of the properties that the computed property depends on `var`, you have to add code that invalidates the cached value which is a performance and a complexity hit for your struct.<br class=""></blockquote><br class="">The performance hit is likely a bit larger if you *don't* use a mutable property and instead create a whole new instance.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">How is<span class="Apple-converted-space">&nbsp;</span></span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&nbsp;&nbsp;&nbsp;let a = SomeStruct()</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&nbsp;&nbsp;&nbsp;var b = a</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">not creating a new instance?</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div>Of course this creates a new instance. &nbsp;But it’s not what I was talking about. &nbsp;What I was distinguishing is the performance characteristics of pure functional code that simply modifies one or more properties like this:</div><div><br class=""></div><div>let x = Person(firstName: “John”, lastName: “Black”)</div><div><br class=""></div><div>// don’t pay attention to syntax - any syntax with the same semantics&nbsp;</div><div>// would have the same performance characteristics</div><div>let y = Person(firstName: x.firstName, lastName: “White”)</div><div><br class=""></div><div>Relative to code that leverages the mutability model of Swift’s value types:</div><div><br class=""></div><div>var x = Person(firstName: “John”, lastName: “Black”)</div><div>x.lastName = “White"</div><div><br class=""></div><div>The former is usually going to be slower because it requires copying, additional storage, and re-runs the entire initializer despite only modifying a single property. &nbsp;Sometimes the optimizer may be able to eliminate the difference, but that is not always going to be the case.</div><div><br class=""></div><div>Your observation that a lot of boilerplate is required if you need to cache a derived property is very fair. &nbsp;That is a legitimate problem that would be reasonable to solve.</div><div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Anyway, the cost depends on how expensive the calculation for the calculated property is and how often you use it and how well the compiler can optimise copies of immutable objects.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">On the other hand, making a property that is not supposed to change over the lifetime of the object a let property is self documenting and not to be avoided IMO.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><div><br class=""></div><div>This is perfectly fair. &nbsp;But if you need the ability to construct a nearly identical instance with only one, or maybe a few, modified properties you probably have code that would benefit from using Swift’s model for mutability of value types.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><br class="">It might be interesting to think about language solutions to reduce this complexity. &nbsp;&nbsp;But in general, the mutability model of Swift's value types is an asset and should be embraced, not avoided. &nbsp;That's what a "Swifty" solution would do IMO.<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Yeah, I really hate it when people say “x is Swifty” or “y is not Swifty”. What is Swifty or not usually depends on what the person saying it prefers. On the other hand, most programmers i have come across agree that writing code that is self documenting is good practice and therefore using let instead of var for properties that never change over the life time of the object counts in that respect in my opinion.</span></div></blockquote><div><br class=""></div><div>“Swifty” was followed by IMO so it was clearly a statement of my personal opinion. &nbsp;I agree that self documenting code is a great practice. &nbsp;</div><div><br class=""></div><div>On the other hand there isn’t an increase in understanding of the semantics of a struct property by using &nbsp;`let` instead of `var` if that property will often be modified via an initializer that effectively creates a copy of a previous instance, but with a new value for said property. &nbsp;The differences between `let` and `var` in this case are pretty subtle and not usually consequential.</div><div><br class=""></div><div>There are certainly times when using let rather than var for members of a struct makes sense. &nbsp;An identifier is a great example where let is usually appropriate. &nbsp;But in *my* opinion there is a widespread misunderstanding of the semantic difference between let and var for structs.</div><div><br class=""></div><div>To demonstrate, in the following example both implementations of `updateOwner` have identical semantics:</div><div><br class=""></div><div>class Foo {</div><div>&nbsp; &nbsp; var owner: Person</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; // pure functional style</div><div>&nbsp; &nbsp; func updateOwner(newFirstName: String) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;owner&nbsp;= Person(firstName: newFirstName, lastName: owner.lastName)</div><div>&nbsp; &nbsp; }</div><div><br class=""></div><div>&nbsp; &nbsp; // using Swift’s model for value type mutation, obviously only works if `firstName` is declared with `var`</div><div><div>&nbsp; &nbsp; func updateOwner(newFirstName: String) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;owner.firstName = newFirstName</div><div>&nbsp; &nbsp; }</div></div><div>}</div><div><br class=""></div><div>If your type is intended to be used in code with semantics like this there is no significant benefit to the pure functional style. &nbsp;Today there is a price in terms of syntax which could be solved provided sufficient motivation. &nbsp;There is also a price in terms of performance that may sometimes be optimized away, but not always.</div><div><br class=""></div><div>I’m not necessarily suggesting that one style or the other should always be preferred. &nbsp;What I am suggesting is that we should be clear about understanding what the tradeoffs are. &nbsp;And I am also suggesting that there are enough benefits to the mutability model for value types that identifying challenges with writing code that relies on this model (such as caching a computed property) is a more fruitful avenue for improving the language than offering syntactic sugar for more functional, perhaps lens-y style code. &nbsp;That’s not to say we can’t do both, but we do need to prioritize.</div><br class=""><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></blockquote></blockquote></div></blockquote></div><br class=""></body></html>